Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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的01矩阵中,起点在左上角,出口在右下角。求一条路径,是路径表示的二进制数的值最小,输出这个数的二进制。
思路:从起点出发,在‘0’上进行bfs,找到一个或多个里出口曼哈顿距离最近的点,从这种点走字典序最小的最短路到出口,这条路径就是所求的路径。
前面的bfs都没什么问题,关键是后面的路径怎么找,由于要求字典序最小,可以由出口进行状压dp得到。但是,位数太多了,只能自己建立一个状压类型,见代码的struct Bit。但是,存下所有点的状态,内存是不够的,动态分配内存又太慢,所有采用数组分配内存的方式,再用模拟栈来回收内存。就这样乱搞的方法完美解决了这题。
#include<cstring>
#include<queue>
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
typedef unsigned __int128 uL;
struct Bit
{
uL mem[16];
inline void init()
{
memset(mem,0,sizeof(mem));
}
inline void set()
{
memset(mem,-1,sizeof(mem));
}
inline void modify(int p)
{
register int x=p>>7;
register int y=p&127;
mem[x]|=(uL)1<<y;
}
inline void print()
{
register int p=2047;
for(; p>=0; p--)
{
register int x=p>>7;
register int y=p&127;
if(mem[x]&((uL)1<<y)) break;
}
for(;p>=0;p--)
{
register int x=p>>7;
register int y=p&127;
if(mem[x]&((uL)1<<y)) putchar('1');
else putchar('0');
}
putchar('\n');
}
inline bool operator < (const Bit &b) const
{
for(register int i=15;i>=0;i--)
{
if(mem[i]==b.mem[i]) continue;
else return mem[i]<b.mem[i];
}
return 0;
}
};
const int mmax = 1010;
char G[mmax][mmax];
bool vis[mmax][mmax];
const int inf = 0x3fffffff;
int n,m;
inline bool in(register int x,register int y)
{
return 0<=x && x<n &&0<=y && y<m;
}
int dir[2][2]={{1,0},{0,1}};
int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void bfs(int x,int y)
{
memset(vis,0,sizeof vis);
queue< pair<int,int> >q;
q.push(make_pair(x,y));
vis[x][y]=1;
while(!q.empty())
{
pair<int,int> x=q.front();
q.pop();
if(G[x.first][x.second]=='1')
continue;
for(int i=0;i<4;i++)
{
int tx=x.first+d[i][0];
int ty=x.second+d[i][1];
if(in(tx,ty) && !vis[tx][ty])
{
vis[tx][ty]=1;
q.push(make_pair(tx,ty));
}
}
}
}
inline int manhandun(int x,int y)
{
return (n+m-x-y-2);
}
int STA[8000];
Bit DP[8000];
int MAP[mmax][mmax];
int cntSTA;
void init()
{
cntSTA=7999;
for(int i=1;i<8000;i++) STA[i]=i;
memset(MAP,0,sizeof(MAP));
}
inline void get(int &a)
{
a=STA[cntSTA--];
}
inline void fre(int &a)
{
STA[++cntSTA]=a;
a=0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",G[i]);
bfs(0,0);
if(vis[n-1][m-1])
{
printf("%c\n",G[n-1][m-1]);
continue;
}
int ans=inf;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(vis[i][j])
{
ans=min(ans,manhandun(i,j));
}
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(vis[i][j] && ans!=manhandun(i,j))
{
vis[i][j]=0;
}
}
get(MAP[n-1][m-1]);
DP[MAP[n-1][m-1]].init();
if(G[n-1][m-1]=='1')
{
DP[MAP[n-1][m-1]].modify(manhandun(n-1,m-1));
}
for(register int i=1;i<=ans;i++)
{
for(register int x=0,y=n+m-2-i;y>=0;x++,y--)
{
if(in(x,y))
{
get(MAP[x][y]);
DP[MAP[x][y]].set();
for(int j=0;j<2;j++)
{
register int tx=x+dir[j][0];
register int ty=y+dir[j][1];
if(in(tx,ty))
{
DP[MAP[x][y]]=min(DP[MAP[x][y]],DP[MAP[tx][ty]]);
if(j==1) fre(MAP[tx][ty]);
}
}
if(G[x][y]=='1') DP[MAP[x][y]].modify(manhandun(x,y));
}
}
}
Bit Ans;
Ans.set();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(vis[i][j])
{
Ans=min(Ans,DP[MAP[i][j]]);
}
}
Ans.print();
}
return 0;
}