2 2 2 11 11 3 3 001 111 101
111 101
#include <bits/stdc++.h> #define maxn 1000 + 10 using namespace std; typedef pair<int , int> P; int go[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1} }; int T, n, m; int vis[maxn][maxn]; char s[maxn][maxn]; int sx, sy; int judge(int x, int y) { if(x > 0 && y > 0 && x <= n && y <= m) return 1; return 0; } void bfs() { queue<P> Q; Q.push(P(1, 1)); while(!Q.empty()) { P p = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { int x, y; x = p.first + go[i][0]; y = p.second + go[i][1]; // printf("%d--%d\n", x, y); if(!judge(x, y) || vis[x][y]) continue; vis[x][y] = 1; if(s[x][y] == '0') Q.push(P(x, y)); if(sx + sy < x + y) { sx = x; sy = y; } } } //cout<<sx<<" * "<<sy<<endl; } int main() { scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i= 1; i <= n; i++) scanf("%s" , s[i] + 1 ); memset(vis, 0, sizeof vis); sx = sy = 1; vis[1][1] = 1; if(s[1][1] == '0') bfs(); if(s[sx][sy] == '0') putchar('0'); else { int f1 , f2 ; f1 = f2 = 0; putchar('1'); //cout<<sx<<" "<<sy<<endl; for(int step = sx + sy; step < n + m; step++) { int x, y; f1 = 0; for(x = 1; x <= n; x++) { y = step - x; if(!judge(x, y) || !vis[x][y]) continue; if(f2 && s[x][y] == '1') continue; int xx, yy; for(int i = 0; i < 2; i++) { xx = x + go[i][0]; yy = y + go[i][1]; if(!judge(xx, yy)) continue; vis[xx][yy] = 1; if(s[xx][yy] == '0') f1 = 1; } } f2 = f1; putchar(f1 ? '0' : '1'); } } putchar('\n'); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dojintian/article/details/47188475