您的位置:首页 > 其它

2015 多校联赛 ——HDU5335(Walk out)

2015-07-31 17:28 148 查看

Walk Out

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2001 Accepted Submission(s): 376



Problem Description

In an n∗m 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 (1≤n,m≤1000).
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

从矩阵左上角走到右下角,而且经过的数字会被以二进制的方式记录下来,求最小

思路:如果第一个位置是1,只能一直走下 或者 右,找出最小值

如果第一个位置是0,找到离右下角最近的1,然后进行上面步骤

但是当时并没有实现,超时了,感觉有点麻烦 - -,用搜索不知道怎么记录值,而且容易超

标码中的用for循环求解,感觉好机智(⊙o⊙)…,找到最近的位置 x + y,找出这些中最小的值输出,再讲它们进行标记。

P:每次离右下角i + j的点是那些固定的,在这些固定的周围继续找。

必须扩充栈用C++,否则爆掉。 自己测试时就说怎么一直有问题 TAT

#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

int T, n, m,tot;
char S[1100][1100];
bool used[1100][1100];
char ch = '0';
char ch1 = '1';

void bfs(int x,int y)
{
if(used[x][y])
return ;
used[x][y] = true;

if(S[x][y] == ch1)
return;

if (x>1) bfs(x-1,y);
if (x<n) bfs(x+1,y);
if (y>1) bfs(x,y-1);
if (y<m) bfs(x,y+1);
}

int main()
{
//freopen("9.txt", "r", stdin);

scanf("%d",&T);
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%s", S[i] + 1);
for (int i = 0; i <= n + 1; i++)    //预处理
S[i][0] = '2', S[i][m + 1] = '2';
for (int i = 0; i <= m + 1; i++)
S[0][i] = '2', S[n + 1][i] = '2';
memset(used,false,sizeof(used));
tot  = 0;
bfs(1,1);

if(used
[m] && S
[m] == ch)
{
printf("0\n");//全是0
continue;
}

for(int i= 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(used[i][j])
tot = max(tot,i+j);

printf("1");
for (int i = tot; i < n + m; i++)
{
char mi = '1';
for (int j = 1; j <= n; j++)
if (1 <= i - j && i - j <= m && used[j][i - j])
{
mi = min(mi, S[j + 1][i - j]);
mi = min(mi, S[j][i - j + 1]);
}
printf("%c", mi);
for (int j = 1; j <= n; j++)
if (1 <= i - j && i - j <= m && used[j][i - j])
{
if (S[j + 1][i - j] == mi) used[j + 1][i - j] = true;
if (S[j][i - j + 1] == mi) used[j][i - j + 1] = true;
}
}
printf("\n");
}

return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: