Please notice the unusual memory limit of this problem.

Orac likes games. Recently he came up with the new game, “Game of Life”.

You should play this game on a black and white grid with n rows and m columns. Each cell is either black or white.

For each iteration of the game (the initial iteration is 0), the color of each cell will change under the following rules:

If there are no adjacent cells with the same color as this cell on the current iteration, the color of it on the next iteration will be the same.
Otherwise, the color of the cell on the next iteration will be different.
Two cells are adjacent if they have a mutual edge.

Now Orac has set an initial situation, and he wants to know for the cell (i,j) (in i-th row and j-th column), what will be its color at the iteration p. He may ask you these questions several times.

Input
The first line contains three integers n,m,t (1≤n,m≤1000,1≤t≤100000), representing the number of rows, columns, and the number of Orac queries.

Each of the following n lines contains a binary string of length m, the j-th character in i-th line represents the initial color of cell (i,j). ‘0’ stands for white, ‘1’ stands for black.

Each of the following t lines contains three integers i,j,p (1≤i≤n,1≤j≤m,1≤p≤1018), representing a query from Orac.

Output
Print t lines, in i-th line you should print the answer to the i-th query by Orac. If the color of this cell is black, you should print ‘1’; otherwise, you should write ‘0’.

Examples
Input
3 3 3
000
111
000
1 1 1
2 2 2
3 3 3
Output
1
1
1
Input
5 2 2
01
10
01
10
01
1 1 4
5 1 4
Output
0
0
Input
5 5 3
01011
10110
01101
11010
10101
1 1 4
1 2 3
5 5 3
Output
1
0
1
Input
1 1 3
0
1 1 1
1 1 2
1 1 3
Output
0
0
0
Note

For the first example, the picture above shows the initial situation and the color of cells at the iteration 1, 2, and 3. We can see that the color of (1,1) at the iteration 1 is black, the color of (2,2) at the iteration 2 is black, and the color of (3,3) at the iteration 3 is also black.

For the second example, you can prove that the cells will never change their colors.
思路:讲真的,比D题思维量小一些。
我们对样例按照题意模拟一下,我们是可以发现规律的。当达到某一状态的时候,之后的状态就可以确定了。我们第一轮符合题意的点放入队列中,利用bfs去扩展。直到全部扩展完成或者无法扩展就退出。bfs的过程记录每一个格子会发生变化的回合,之后的变化就是有规律可寻的了。然后q次询问,就可以O(1)的时间内完成了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e3+10;
struct node{int x,y;node(){}node(int a,int b){x=a,y=b;}
};
int mp[maxx][maxx];
int vis[maxx][maxx];
int d[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,q;inline void bfs()
{int i,j,k,tx,ty;node a;queue<node> q;for(i=1;i<=n;i++)for(j=1;j<=m;j++)for(k=0;k<4;k++) {tx=i+d[k][0];ty=j+d[k][1];if(tx<1||tx>n||ty<1||ty>m) continue;if(mp[i][j]==mp[tx][ty]) vis[i][j]=1,q.push(node(i,j));}while(q.size()){a=q.front();q.pop();for(i=0;i<4;i++){tx=a.x+d[i][0];ty=a.y+d[i][1];if(tx<1||tx>n||ty<1||ty>m||vis[tx][ty]) continue;if(mp[tx][ty]==(!mp[a.x][a.y])){vis[tx][ty]=vis[a.x][a.y]+1;q.push(node(tx,ty));}}}
}
int main()
{scanf("%d%d%d",&n,&m,&q);for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%1d",&mp[i][j]);memset(vis,0,sizeof(vis));bfs();int x,y;ll t;while(q--){scanf("%d%d%lld",&x,&y,&t);if((ll)vis[x][y]>t) printf("%d\n",mp[x][y]);else{if(vis[x][y]==0) printf("%d\n",mp[x][y]);else{t-=vis[x][y];printf("%d\n",mp[x][y]^((t+1ll)&1ll));}}}return 0;
}

努力加油a啊,(o)/~

Orac and Game of Life CodeForces - 1350E(思维+BFS)相关推荐

  1. CodeForces - 1350E Orac and Game of Life(bfs)

    题目链接:点击查看 题目大意:给出一个 n * m 的 01 矩阵,矩阵每一秒都会迭代,迭代规则如下: 如果对于 ( x , y ) 格子而言,四周相邻的格子 ( xx , yy ) 均满足 maze ...

  2. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  3. Codeforces Round #636 (Div. 3) E. Weights Distributing 思维 + bfs

    传送门 文章目录 题意: 思路: 题意: n≤2e5,m≤2e5n\le2e5,m\le2e5n≤2e5,m≤2e5 思路: 怎么感觉每场div3div3div3都有一个巧妙的图论题. 首先如果只有两 ...

  4. codeforces - 1315C - 思维题

    原题链接:https://codeforces.com/problemset/problem/1315/C 翻译: 这是一个猜谜游戏,你需要猜中一个序列,谜题是一个序列,我们设为b,长度为n.你需要根 ...

  5. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  6. Codeforces 1093C (思维+贪心)

    题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...

  7. B. Bogosort codeforces(思维)

    outputstandard output You are given an array a1,a2,-,an. Array is good if for each pair of indexes i ...

  8. Count Subrectangles CodeForces - 1323B(思维)

    You are given an array a of length n and array b of length m both consisting of only integers 0 and ...

  9. Dead Pixel CodeForces - 1315A(思维)

    Screen resolution of Polycarp's monitor is a×b pixels. Unfortunately, there is one dead pixel at his ...

最新文章

  1. archlinux详细安装步骤_Archlinux 安装教程图文详解
  2. Linux环境下查PG库的慢sql,postgresql慢SQL
  3. 在visual studio中使用git版本系统(zz)
  4. Apache Kafka:下一代分布式消息系统
  5. 移动端 Web 开发踩坑之旅
  6. Console-算法[for]-打印出杨辉三角形
  7. return两个返回值_23.1 函数的返回值(二)
  8. ECharts 饼图 legend 样式修改
  9. graphics | 基础绘图系统(三)——添加文本标注、坐标轴线和图例
  10. [CMake] include_directories 和 target_include_directories
  11. 动易CMS2006安装与配置
  12. HBase安装与验证
  13. luogu P1375 小猫
  14. 从限定词开始 - 词性识别在人工智能自然语言处理中的不足与改进
  15. 第二空间计算机最新破解,我的J4125黑群晖折腾记 - 软件篇:无需U盘引导及固态硬盘扩充空间容量法...
  16. 开源生态|打造活力开源社区,共建开源新生态!
  17. 人工智能可以像人类一样学习吗?
  18. 浏览器安装公众号编辑器
  19. ioi2016aliens
  20. 博客网站更换模板及插件使用

热门文章

  1. IOS开发简易的网易新闻页面
  2. a=10a=0C语言,C语言程序设计10A卷试题及答案.doc
  3. onnx模型转tensorflow模型
  4. python深浅拷贝的底层理解_理解python中的深拷贝与浅拷贝
  5. 计算机基础说课 word,计算机基础说课稿
  6. java map中的entry_java中Map及Map.Entry详解(组图)
  7. java xml获取属性值_java – 如何获取具体属性值的特定XML元素?
  8. 链接器相关的一些基本问题
  9. 第六天,字典Dictionary
  10. lua C++ wrapper