Walk Out

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

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
 
 
题目大意:t组数据。每组n,m分别表示行和列。下面的n行m列用0、1表示地图情况。问你从(1,1)走到(n,m)所经过的点按顺序形成的二级制数最小是多少?
 
 
解题思路:先把所有的跟入口相连的0连通块都标记上。然后找到跟这个0连通块相邻接的1.这些1是可能的出发点。再在这些1中找到离出口最近的点。从这些点只需要往右往下走。因为当你确定离出口最近的点的时候,二进制数的位数已经确定。然后再广搜,贪心0。如果广搜时发现有0,那么将所有的0入队,如果没有发现0,那么将所有的1入队。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=0x3f3f3f3f;
int vis[maxn][maxn];
int Map[maxn][maxn];
int way[2*maxn];
int f[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,k;
struct NODE{
int x,y;
};
queue<NODE>Q;
stack<NODE>S;
int BFS(){
NODE st,tmp;
while(!Q.empty()){
st=Q.front();
Q.pop();
int xx,yy;
for(int i=0;i<4;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])&&Map[xx][yy]==0){
if(xx==n&&yy==m)
return -1;
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=1;
Q.push(tmp);
}
}
}
}
bool check(int x,int y){
int xx,yy;
for(int i=0;i<4;i++){
xx=x+f[i][0];
yy=y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==1){
return true;
}
}
return false;
}
void Find1(int typ){
while(!S.empty())
S.pop();
while(!Q.empty())
Q.pop();
int maxs=-INF;
NODE st,tmp; if(typ==0){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(Map[i][j]==1){
if(check(i,j)){ if(i+j>maxs){
maxs=i+j;
}
st.x=i,st.y=j;
vis[i][j]=2;
S.push(st);
}
}
}
}
}else{
st.x=1,st.y=1;
vis[1][1]=2;
maxs=2;
S.push(st);
}
if(S.empty()==0)
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
if(st.x+st.y==maxs){
Q.push(st);
}
}
while(1){
int flag=0;
while(!Q.empty()){
st=Q.front();
Q.pop();
if(st.x==n&&st.y==m){
return ;
}
int xx,yy;
for(int i=0;i<2;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])){
if(Map[xx][yy]==0){
flag=1;
}
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=2;
S.push(tmp);
}
}
}
if(flag==1){
way[k++]=0;
while(!S.empty()){
st=S.top();
S.pop();
if(Map[st.x][st.y]==0){
Q.push(st);
}
}
}else{
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
Q.push(st);
}
}
}
}
int main(){
int t;
char str[1020];
scanf("%d",&t);
while(t--){
k=0;
memset(vis,0,sizeof(vis));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",str+1);
for(int j=1;j<=m;j++){
Map[i][j]=str[j]-'0';
}
}
if(n==m&&n==1&&Map[1][1]==0){
printf("0\n");continue;
}
if(Map[1][1]==0){
NODE st;
st.x=1,st.y=1;
vis[1][1]=1;
while(!Q.empty())
Q.pop();
Q.push(st);
if(BFS()==-1) {
printf("0\n");
continue;
}
Find1(0);
}
else{
Find1(1);
}
for(int i=0;i<k;i++){
printf("%d",way[i]);
}printf("\n");
}
return 0;
}
/*
50
3 3
001
111
101 55
1 2
01 */

  

 
 

最新文章

  1. Oracle基础——学习笔记
  2. MDI窗体容器、权限设置
  3. Android开发-API指南- Calendar Provider
  4. 我是面试官--&quot;自我介绍&quot;
  5. 分享一个解析XML成为php数组的方法
  6. Salesforce 小知识:大量“子记录”的处理方法
  7. menu
  8. Python3:几行代码实现阶乘
  9. vue常考面试题
  10. mysql兼容emoji表情存取
  11. jquery的jsonp的使用
  12. mysql 修改文件记录:
  13. mysql快速生成truncate脚本清空数据库表记录
  14. spring cloud 学习
  15. [转载]MACD 各周期指标状态
  16. 问题2:input、textarea、submit 宽度设置为100%,但显示宽度不一致
  17. java语言编程入门
  18. 连接mysql提示Establishing SSL connection without server&#39;s identity verification is not recommended错误
  19. [Big Data] Week4B (Basic)
  20. java poi处理新版xlsx后缀的excel

热门文章

  1. Web Server Jexus配置及使用
  2. Binder学习笔记(二)——defaultServiceManager()返回了什么?
  3. [MOOC程序设计与算法二] 递归二
  4. Mysql 5.6主从搭建
  5. ajaxs
  6. 【沽泡学院07】基于ElasticSearch搜索附近的人
  7. 给label添加点击事件
  8. 小程序scroll-view采坑
  9. @AutoConfigureAfter不生效 @Configration bean的创建顺序
  10. linux 网卡配置文件详解2018-03-07