关闭

Hdu-5335 Walk Out (BFS+贪心)

225人阅读 评论(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
查看评论

[bfs+贪心] 多校第四场 HDU5335 Walk Out

Walk OutTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1224 Accepted Submission(s): 229P...
  • u012848631
  • u012848631
  • 2015-07-31 10:29
  • 354

hdu5335Walk Out bfs+贪心

//n行长度为m的01串, //从(1,1)走到(n,m)的所经过的点中形成的二进制数最小的为多少 //对于第一位是1,则只能往下和往右走, //每次处理距离终点距离一样的点,如果这样的点中有0那么只走0的点 //如果没有0的点,那么都要走 //对于第一位是0,那么先找到距离终点距离最近的1,再从那...
  • cq_pf
  • cq_pf
  • 2015-08-05 09:19
  • 440

多校第四场 1009 hdu 5335 Walk Out(bfs+贪心)

题目连接:hdu 5335题目大意:给出一个01矩阵,从左上角走到右下角,标记路径,问路径得到的二进制数最小是多少?题目分析: 利用bfs找出到达右下角的曼哈顿距离最近的为0的点,因为当出现1之后,一定是通过曼哈顿路径才能得到最小解,因为任何多余的路径都会导致数位变多,而导致数大于路径短于当前路径组...
  • qq_24451605
  • qq_24451605
  • 2015-07-31 10:00
  • 392

HDOJ 5335 Walk Out 贪心+BFS

BFS沿着0走,记录下最靠近终点的1 然后斜着扫描 Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot...
  • u012797220
  • u012797220
  • 2015-08-01 21:22
  • 489

HDU 5335 Walk Out (BFS后贪心)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5335 题意:给出一个n*m的地图,你要从最左上角的格子走到最右下角的格子,每个格子上有0或1,没到一个格子你要按顺序记下上面的数字,最后得到一个01串,这个01串是一个二进制数,请输出你能得到的最小的二进制...
  • qq_24833289
  • qq_24833289
  • 2015-08-15 20:04
  • 182

【POJ】5335 - Walk Out 【BFS + 贪心】

题目 Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3035 &...
  • xianyun2009
  • xianyun2009
  • 2015-08-04 10:59
  • 340

HDU 5335 Walk Out(搜索+贪心)

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2810 ...
  • yeguxin
  • yeguxin
  • 2015-08-03 10:00
  • 386

HDU 5335 - Walk Out (DFS + 贪心)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=5335 题意: n*m的01矩阵,起点(1,1),终点(n,m)。求出走出的路径得到的最小的二进制。 思路: 贪心策略: 一开始能走0就走0,dfs走到离终点最近的0。接下来的路径应该尽可能...
  • u013534690
  • u013534690
  • 2015-07-31 15:34
  • 366

hdu 5335 Walk Out 搜索+贪心

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 141  &...
  • firenet1
  • firenet1
  • 2015-07-30 17:55
  • 1252

AtCoder Beginner Contest 085 D Katana Thrower(贪心)

Problem Statement You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …...
  • finalcsdn
  • finalcsdn
  • 2018-01-07 22:03
  • 398
    个人资料
    • 访问:111284次
    • 积分:4547
    • 等级:
    • 排名:第7749名
    • 原创:363篇
    • 转载:2篇
    • 译文:0篇
    • 评论:42条
    最新评论