2015 Multi-University Training Contest 4 hdu 5335 Walk Out

Walk Out

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 639    Accepted Submission(s): 114


Problem Description
In an  nm maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it.

An explorer gets lost in this grid. His position now is (1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position (1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.
 

 

Input
The first line of the input is a single integer  T (T=10), indicating the number of testcases. 

For each testcase, the first line contains two integers n and m (1n,m1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.
 

 

Output
For each testcase, print the answer in binary system. Please eliminate all the preceding  0 unless the answer itself is 0 (in this case, print 0 instead).
 

 

Sample Input
2
2 2
11
11
3 3
001
111
101
 

 

Sample Output
111
101
 

 

Author
XJZX
 

 

Source
 
解题:搜索,先看一直走0,走到最靠近终点的1,然后再从这个1所在半径,斜对角线似的搜索,
 
 1 #include <bits/stdc++.h>
 2 #define pii pair<int,int>
 3 using namespace std;
 4 const int maxn = 1005;
 5 const int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 6 char mp[maxn][maxn];
 7 int n,m,sx,sy;
 8 queue< pii >q;
 9 bool vis[maxn][maxn];
10 bool isIn(int x,int y) {
11     return x >= 0 && x < n && y >= 0 && y < m;
12 }
13 void bfs(int x,int y) {
14     while(!q.empty()) q.pop();
15     q.push(pii(x,y));
16     vis[x][y] = true;
17     while(!q.empty()) {
18         pii now = q.front();
19         q.pop();
20         for(int i = 0; i < 4; ++i) {
21             int nx = now.first + dir[i][0];
22             int ny = now.second + dir[i][1];
23             if(!isIn(nx,ny) || vis[nx][ny]) continue;
24             vis[nx][ny] = true;
25             if(mp[nx][ny] == '0') q.push(pii(nx,ny));
26             if(sx + sy < nx + ny){
27                 sx = nx;
28                 sy = ny;
29             }
30         }
31     }
32 }
33 int main() {
34     int kase;
35     scanf("%d",&kase);
36     while(kase--) {
37         scanf("%d%d",&n,&m);
38         for(int i = 0; i < n; ++i) scanf("%s",mp[i]);
39         sx = sy = 0;
40         memset(vis,false,sizeof vis);
41         vis[0][0] = true;
42         if(mp[0][0] == '0') bfs(0,0);
43         if(mp[sx][sy] == '0') puts("0");
44         else {
45             bool nowflag = false;
46             putchar('1');
47             for(int i = sx + sy; i < n + m - 2; ++i){
48                 bool flag = false;
49                 for(int k = 0; k <= i; ++k){
50                     int x = k;
51                     int y = i - k;
52                     if(!isIn(x,y) || !vis[x][y]) continue;
53                     if(nowflag && mp[x][y] == '1') continue;
54                     for(int j = 0; j < 2; ++j){
55                         int nx = x + dir[j][0];
56                         int ny = y + dir[j][1];
57                         if(!isIn(nx,ny)) continue;
58                         vis[nx][ny] = true;
59                         if(mp[nx][ny] == '0') flag = true;
60                     }
61                 }
62                 nowflag = flag;
63                 putchar(flag?'0':'1');
64             }
65             putchar('\n');
66         }
67     }
68     return 0;
69 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4690662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值