/***************************************** Author :Crazy_AC(JamesQi) Time :2016 File Name : 给定一个n*m的01矩阵,问从左上角走到右下角这个路径中经过的01构成一个二进制串,求最小的串; 思路:串的长度必然 < n + m; 尽量让前面的1出现得更晚,也就是0尽量的长。<span style="color:#ff0000;">对于这种二维坐标,边距为1的图的bfs是 点p(x, y),搜索特点是(x + y)从2到n+m的增长,先搜索完x+y=l的所有点,才会搜索x+y=l+1的点</span>; ###############
正好就像是从左到右的找01值。
上面这么一个串,从左到右先找能不能是0先出现,若某个位置出现了0,就标记起来, 然后起后面的搜索就不再进行了。搜过的位置不再搜索。 这样的搜索结果就是最优的了,具体看代码解释。 *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back #define lson rt << 1 #define rson rt << 1 | 1 #define bug cout << "BUG HERE\n" typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-10; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int dx[] = {0, 1, -1, 0};//left,down,up,right int dy[] = {1, 0, 0, -1}; int nCase = 0; const int maxn = 1010; char s[maxn][maxn]; bool mark[maxn][maxn]; int A[maxn+maxn]; int R[maxn][maxn]; int D[maxn+maxn]; vector<ii> vec; int n, m; void bfs() { queue<ii> que; // memset(mark, false,sizeof mark); vec.clear(); vec.push_back(ii(1, 1)); que.push(ii(1, 1)); mark[1][1] = true; int ans = 2; while(!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); for (int i = 0;i < 4;++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx <= 0 || nx > n || ny <= 0 || ny > m || mark[nx][ny] || s[nx][ny] == '1') continue; mark[nx][ny] = true; que.push(ii(nx, ny)); if (nx + ny > ans) { vec.clear(); ans = nx + ny; } if (nx + ny == ans) vec.push_back(ii(nx, ny)); } } } void solve() { queue<ii> que; if (s[1][1] == '1') que.push(ii(1, 1)); else { // bug; bfs(); for (int i = 0;i < vec.size();++i) que.push(vec[i]); } // printf("(%d,%d)\n",que.front().first, que.front().second); while(!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); if (D[x + y] && s[x][y] == '1') continue;//(x+y)这个位置出先了0,且(x,y)这里是1,所以就不用再往后搜索了。 for (int i = 0;i < 2;++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx <= 0 || nx > n || ny <= 0 || ny > m) continue; if (mark[nx][ny]) continue;//走过的不再走。 // bug; // printf("(%d,%d)\n", nx, ny); // mark[nx][ny] = true; R[nx][ny] = 3 - i;//纪录路径方便打印。 if (D[nx + ny] == 0 && s[nx][ny] == '0') D[nx + ny] = 1; if (!mark[nx][ny]) { mark[nx][ny] = true; que.push(ii(nx, ny)); } } } } void put() { int x = n, y = m, c = 0; while(true) { A[c++] = s[x][y]; if (R[x][y] == -1) break; int t = R[x][y]; x = x + dx[t]; y = y + dy[t]; } while(c && A[c - 1] == '0')c--; if (c == 0) { puts("0"); return ; } for (int i = c - 1;i >= 0;--i) putchar(A[i]); puts(""); } int main(int argc, const char * argv[]) { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(R, -1,sizeof R); memset(D, 0,sizeof D); memset(mark, false,sizeof mark); for (int i = 1;i <= n;++i) scanf("%s",s[i] + 1); solve(); put(); } return 0; }参考了网上的代码QAQ