Hdu-5335 Walk Out (BFS+贪心)

116 篇文章 0 订阅
50 篇文章 0 订阅
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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5711  5710  5709  5708  5707


题意:给你一个n*m的零一矩阵,起点在左上角终点在右下角,没走过一步记录下脚下的数字,最后求按二进制系统 最小的路径。


分析:考虑起始位置是不是0,若是0则BFS出所有离起点曼哈顿距离最远的点否则以(1,1)作为起点,BFS最小路径。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int f[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
char S[1005][1005];
struct thing
{
	int x,y,v,f;
} q[1000007],Nq[1000007];
int T,n,m,s,t,ans[2010];
bool jud[1005][1005],F[2010];
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		memset(jud,0,sizeof(jud));
		memset(F,0,sizeof(F));
		scanf("%d%d",&n,&m);
		for(int i = 1;i <= n;i++) scanf("%s",S[i]+1);
		if(S[1][1] == '0')
		{
			s = 1,t = 2;
			q[s].x = 1,q[s].y = 1;
			jud[1][1] = true;
			int Max = 2;
			while(s != t)
			{
				for(int k = 0;k < 4;k++)
				{
					int Nx = q[s].x + f[k][0],Ny = q[s].y + f[k][1];
					if(Nx && Nx <= n && Ny && Ny <= m && S[Nx][Ny] == '0')
					{
						q[t].x = Nx;
						q[t].y = Ny;
						Max = max(Max,Nx+Ny);
						if(!jud[q[t].x][q[t].y])
						{
							jud[q[t].x][q[t].y] = true;
							t++;
						}
					}
				}
				s++;
			}
			t--;
			s = 1;
			bool flag = false;
			while(t)
			{
				if(q[t].x == n && q[t].y == m) 
				{
					cout<<0<<endl;
					flag = true;
					break;
				}
				if (q[t].x+q[t].y == Max)
				{
					if(q[t].x < n)
					{
						Nq[s].x = q[t].x+1;
						Nq[s].y = q[t].y;
						if(!jud[Nq[s].x][Nq[s].y])
						{
							Nq[s].f = 0;
							jud[Nq[s].x][Nq[s].y] = true;
							s++;
						}
					} 
					if(q[t].y < m)
					{
						Nq[s].x = q[t].x;
						Nq[s].y = q[t].y+1;
						if(!jud[Nq[s].x][Nq[s].y])
						{
							Nq[s].f = 0;
							jud[Nq[s].x][Nq[s].y] = true;
							s++; 
						}
					}
				}
				t--;
			}
			if(flag) continue;
			t = s;
			s = 1;
		}
		else 
		{
			s = 1,t = 2;
			Nq[s].x = 1;
			Nq[s].y = 1;
			Nq[s].f = 0;
			jud[1][1] = true;
		}
		while(s != t)
		{
			if(F[Nq[s].x+Nq[s].y] && S[Nq[s].x][Nq[s].y] == '1') 
			{
				s++;
				continue;
			}
			if(Nq[s].x < n)
			{
				int Nx = Nq[s].x+1,Ny = Nq[s].y;
				if(!jud[Nx][Ny])
				{
					jud[Nx][Ny] = true;
					Nq[t].x = Nx;
					Nq[t].y = Ny;
					Nq[t].f = s;
					if(S[Nx][Ny] == '0') F[Nx+Ny] = true;
					t++;
				}
			}
			if(Nq[s].y < m)
			{
				int Nx = Nq[s].x,Ny = Nq[s].y+1;
				if(!jud[Nx][Ny])
				{
					jud[Nx][Ny] = true;
					Nq[t].x = Nx;
					Nq[t].y = Ny;
					Nq[t].f = s;
					if(S[Nx][Ny] == '0') F[Nx+Ny] = true;
					t++;
				}
			}
			s++;
		}
		for(int i = t-1;i;i--)
		 if(Nq[i].x == n && Nq[i].y == m) 
		 {
		 	int now = i,cnt = 0;
			while(now)
			{
				ans[++cnt] = S[Nq[now].x][Nq[now].y];
				now = Nq[now].f;
			} 
			for(int j = cnt;j;j--) cout<<char(ans[j]);
			cout<<endl;
		 	break;
		 }
	}	
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值