传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5335
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2280 Accepted Submission(s): 444
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.
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.
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
Author
XJZX
Source
题意:n*m的格子,由0或1组成,从(1,1)走到(n,m),问路径(01串组成)最小为多少(二进制)。
暴搜不行,考虑每次都是 / ,用优先队列维护。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define sf scanf
const int d[4][2] = {1,0,0,1,-1,0,0,-1};
struct node
{
int x,y;
bool operator<(const node &a)const
{
return x+y>a.x+a.y;
}
};
priority_queue<node> que;
queue<node> que1;
vector<node> vt,vtmp;
bool flag[1005][1005];
int n,m;
int mapt[1005][1005];
bool legel(int x,int y){return x>=1&&x<=n&&y<=m&&y>=1&&mapt[x][y]==0;}
int main()
{
int T;
sf("%d",&T);
while(T--)
{
sf("%d%d",&n,&m);
while(!que.empty())que.pop();
while(!que1.empty())que1.pop();
for(int i =1; i<=n; i++)
{
char ch[1005];
sf("%s",ch);
for(int j=1; j<=m; j++)
{
mapt[i][j]=ch[j-1]-'0';
if(mapt[i][j]==0)que.push(node {i,j});
}
}
if(n==1&&m==1)
{
printf("%d\n",mapt[1][1]);
continue;
}
int pos;
vt.clear();
if(mapt[1][1] == 1)
{
pos = 2;
printf("1");
vt.push_back(node{1,1});
}
else
{
pos = 2;
que1.push(node {1,1});
memset(flag,true,sizeof flag);
flag[1][1] = false;
while(!que1.empty())
{
node tmp = que1.front();
pos = max(pos,tmp.x+tmp.y);
que1.pop();
for(int i = 0; i<=3; i++)
{
int x = tmp.x + d[i][0];
int y = tmp.y + d[i][1];
if(legel(x,y) && flag[x][y])
{
que1.push(node {x,y});
flag[x][y] = false;
}
}
}
node tmp = que.top();
while(tmp.x+tmp.y<pos){que.pop();tmp = que.top();}
while(tmp.x+tmp.y == pos)
{
que.pop();
if(flag[tmp.x][tmp.y] ==false)vt.push_back(node{tmp.x,tmp.y});
if(!que.empty()) tmp = que.top(); else break;
}
}
if(pos == n+m) {puts("0");continue;}
while(!que.empty())
{
node tmp =que.top();
// cout<<"----"<<tmp.x<<","<<tmp.y<<endl;
que.pop();
int t = vt.size();
bool sign = false;
for(int i = 0;i<t;i++)
{
// cout<<"use "<<vt[i].x<<","<<vt[i].y<<endl;
if(tmp.x>=vt[i].x && tmp.y>=vt[i].y) {sign = true;break;}
}
if(sign){
for(int i = pos+1;i<tmp.x+tmp.y;i++) printf("1");
printf("0");
pos = tmp.x+tmp.y;
vtmp.clear();
vtmp.push_back(tmp);
while(!que.empty())
{
tmp = que.top();
if(tmp.x+tmp.y!=pos) break;
que.pop();
// cout<<"do "<<tmp.x<<","<<tmp.y<<endl;
sign = false;
for(int i = 0;i<t;i++)
{
// cout<<"use "<<vt[i].x<<","<<vt[i].y<<endl;
if(tmp.x>=vt[i].x && tmp.y>=vt[i].y) {sign = true;break;}
}
if(sign)vtmp.push_back(tmp);
}
vt.clear();
t = vtmp.size();
for(int i = 0;i<t;i++) vt.push_back(vtmp[i]);
}
}
for(int i = pos;i<n+m;i++)
printf("1");
printf("\n");
}
return 0;
}