HDOJ 5335 Walk Out 贪心+BFS

BFS沿着0走,记录下最靠近终点的1

然后斜着扫描

Walk Out

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2573    Accepted Submission(s): 506

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

Author

XJZX

Source

2015 Multi-University Training Contest 4

/* ***********************************************
Author        :CKboss
Created Time  :2015年08月01日 星期六 20时06分43秒
File Name     :HDOJ5335_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef pair<int,int> pII;
const int maxn=1100;

int n,m;
char mp[maxn][maxn];
bool vis[maxn][maxn];

const int dir_x[4]={1,-1,0,0};
const int dir_y[4]={0,0,1,-1};

bool OK(int x,int y)
{
	if(vis[x][y]==true) return false;
	if((x>=0&&x<n)&&(y>=0&&y<m)) return true;
	return false;
}

bool inside(int x,int y)
{
	if((x>=0&&x<n)&&(y>=0&&y<m)) return true;
	return false;
}

void BFS()
{
	int mx=0;

	queue<pII> q;

	if(mp[0][0]=='0') q.push(make_pair(0,0));
	vis[0][0]=true;

	while(!q.empty())
	{
		pII u=q.front(); q.pop();

		for(int i=0;i<4;i++)
		{
			int x=u.first+dir_x[i];
			int y=u.second+dir_y[i];

			if(OK(x,y)==false) continue;

			vis[x][y]=true;

			if(mp[x][y]=='0')
			{
				q.push(make_pair(x,y));
			}
			else if(mp[x][y]=='1')
			{
				mx=max(mx,x+y);
			}
		}
	}

	if(vis[n-1][m-1]==true)
	{
		putchar(mp[n-1][m-1]);
		putchar(10);
		return ;
	}

	for(int i=mx;i<n+m-1;i++)
	{
		char ch='1';
		for(int x=0;x<n;x++)
		{
			int y=i-x;
			if(inside(x,y)==false||vis[x][y]==false) continue;
			ch=min(ch,mp[x][y]);
		}
		putchar(ch);
		for(int x=0;x<n;x++)
		{
			int y=i-x;

			if(inside(x,y)==false||vis[x][y]==false) continue;
			if(mp[x][y]!=ch) continue;

			if(inside(x+1,y)==true) vis[x+1][y]=true;
			if(inside(x,y+1)==true) vis[x][y+1]=true;
		}
	}
	putchar(10);
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
			scanf("%s",mp[i]);
		memset(vis,false,sizeof(vis));
		BFS();
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 07:14:25

HDOJ 5335 Walk Out 贪心+BFS的相关文章

hdoj 5335 Walk Out

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 1 #include<stdio.h> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN = 1010; 7 char R[MAXN][MAXN]; 8 bool used[MAXN][MAXN]; 9 in

HDU 5335 Walk Out(Bfs搜索字典序最小的最短路)

 题意:nXm的地图, 问通过四个方向从(1,1)走到(1000,1000)所经过的最小二进制序列是多少,忽略前缀0. 思路:首先如果起点为0,那么我们bfs搜索和起点0联通的为0的连通块,这样我们第一步肯定是从与这个连通块相邻的且与重点最近的地方出发. 将所有可能起点加入队列,在bfs一遍找到字典序最小的那条路就是答案, 在这里可以用两个vector类型容器,一个是q2存储所有节点值存为0的结点, 另一个q3存储节点值为1的结点. 那么如果q2不为空那么也就是有可以走零,那么就从这里面选,

【HDOJ 5335】Walk Out

[HDOJ 5335]Walk Out DFS或者BFS 需要极大的剪枝叶否则必超 BFS做的 起点1的话正常搜 0的话从起点找离终点距离最近(x+y最大)的几个点放在队列中然后再搜 搜的时候也要剪枝 用一个变量存放当前搜到的点在最终结果中处在的位置 然后再比较与最近距离还有该点已匹配的字符('1'/'0') 任何一项不优 continue 在注意些特判之类的 A的也挺累... 代码如下: #include <iostream> #include <cstdio> #include

离散化+BFS HDOJ 4444 Walk

题目传送门 1 /* 2 题意:问一个点到另一个点的最少转向次数. 3 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 4 关键理解坐标离散化,BFS部分可参考HDOJ_1728 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <queue> 10 #include <vector> 11 #include

HDU5335——贪心+BFS——Walk Out

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 want

hdu 5335 Walk Out (搜索)

题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走的路线上的0/1组成的二进数最小,现在要为矫情无比的探险家找最优路径咯. 解题思路: 对于二进制数,前导零是对数字大小没有任何影响的.当到不得不走1的时候就只能向下,或者向右走了.所以先搜索出来一直走零,能走到的最靠近终点的位置,然后在类似搜索,找出最优路径. 1 #include <queue>

HDOJ 2037简单的贪心算法

代码: #include<iostream> using namespace std; int main() { int n,s,t1[100],t2[100],i,t,j; while(cin>>n) { if(n==0) break; s=1; for(i=0;i<n;i++) cin>>t1[i]>>t2[i]; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(t2[i]>t2[j]) { t=

HDU 5335 Walk Out (搜索+贪心,超详解)经典

题目链接: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): 2355    Accepted Submission(s): 459 Problem Description In an n?m maze, the righ

HDU 5335——Walk Out——————【贪心】

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).