解题报告 之 HDU5335 Walk Out

解题报告 之 HDU5335 Walk Out

Description

In an  maze,
the right-bottom corner is the exit (position  is
the exit). In every position of this maze, there is either a  or
 written
on it.

An explorer gets lost in this grid. His position now is ,
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 .
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 ,
indicating the number of testcases.

For each testcase, the first line contains two integers  and .
The -th
line of the next  lines
contains one 01 string of length ,
which represents -th
row of the maze.

Output

For each testcase, print the answer in binary system. Please eliminate all the preceding  unless
the answer itself is  (in
this case, print instead).

Sample Input

2
2 2
11
11
3 3
001
111
101 

Sample Output

111
101 

题目大意:给你一个n*m的格子图,每个格子要么是1,要么是0。起点在(1,1),终点在(n,m)。让你找一条路,让途中经过的数字按先后顺序排列组成的二进制数最小。

分析:很容易想到,如果起点是1,那么后面一定只能向右下走了,不然数位多了是第一致命的,然后考虑每一次走优先走0,没有0再走1。所以此时直接广搜到终点就是最短的答案。那么如果起点不是1呢?那么我们就可以先让前面有一些0,直到找到一个1之后又重复第一种情况。所以我们要找的是x+y最大位置1,因为找到1之后我们就要开始优先考虑数位最小了,只能向右下走,所以找到x+y最大的1是保证了位数最小。所以这里需要进行第二次bfs,去将所有(x+y)max的点找到,并依次从这些点开始出发重复第一种情况。

上代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>

using namespace std;
const int MAXN = 1000 + 10;

struct Node
{
	int x, y;
	Node()
	{
	}
	Node( int x, int y )
	{
		this->x = x;
		this->y = y;
	}
};

int vis[MAXN][MAXN];
int zero[MAXN * 4]; //所有x+y的位置上是否存在为0的点
char map[MAXN][MAXN];
int Move[MAXN][MAXN]; //记录路经,方便输出
vector<Node> bg; //所有x+y最大的点
int n, m;

int dir[4][2] = { { 0,1 }, { 1,0 }, { -1,0 }, { 0,-1 } }; //移动方向,注意1和4为一对,2和3为一对

void ini()
{
	memset( vis, 0, sizeof vis );
	memset( zero, 0, sizeof zero );
	memset( Move, -1, sizeof Move );
}

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

void bfs()
//找到x+y最远的点
{
	bg.clear();
	bg.push_back( Node (1, 1) );
	//此时(1,1)为0,先假设为最远的点
	queue<Node> q;
	q.push( Node( 1, 1 ) );
	vis[1][1] = 1;
	int dis = 1; //最小距离

	int x, y;
	while(!q.empty())
	{
		Node tem = q.front();
		q.pop();
		x = tem.x;
		y = tem.y;

		for(int i = 0; i < 4; i++)
		{

			int temx = x + dir[i][0];
			int temy = y + dir[i][1];

			if(!check( temx, temy ) || map[temx][temy] == '1' || vis[temx][temy]) //如果越界或者是1或者已经找过了就不找了
				continue;

			vis[temx][temy] = 1;
			q.push( Node( temx, temy ) );

			if(temx + temy > dis) //发现更远的点
			{
				dis = temx + temy;
				bg.clear();
			}
			if(temx + temy == dis) //发现了最远点
			{
				bg.push_back( Node( temx, temy ) );
			}

		}
	}

}

void bfs2()
//找到从x出发小的数
{
	queue<Node> q;

	if(map[1][1] == '1') //如果起点是1,也优先保证数位最短
	{
		q.push( Node( 1, 1 ) );
	}
	else //如果起点是0,则找到第一个1的最远点
	{
		bfs();
		for(int i = 0; i < bg.size(); i++)
			q.push( bg[i] );
	}

	while(!q.empty()) //从第一个1开始,只向右下走
	{
		Node tem = q.front();
		q.pop();
		int x = tem.x;
		int y = tem.y;

		if(map[x][y] == '1'&&zero[x + y]) //如果当前格子为1,但距离同为 x+y 的点有为0的
		{
			continue;//则跳过
		}

		for(int i = 0; i < 2; i++)
		{
			int temx = x + dir[i][0];
			int temy = y + dir[i][1];

			if(!check( temx, temy )) continue;

			Move[temx][temy] = 3 - i; //记录反方向,方便输出
			if(map[temx][temy] == '0') //x+y的点有为0的
				zero[temx + temy] = 1;
			if(!vis[temx][temy])
			{
				vis[temx][temy] = 1;
				q.push( Node( temx, temy ) );
			}
		}
	}
}

int main()
{
	int kase;
	scanf( "%d", &kase );
	while(kase--)
	{
		ini();
		scanf( "%d%d", &n, &m );
		for(int i = 1; i <= n; i++)
		{
			scanf( "%s", map[i] + 1 );
		}

		bfs2();

		int x = n, y = m;
		int mtem, cnt = 0;
		char ans[MAXN * 4];
		while(true) //方向走,构造结果
		{
			ans[cnt++] = map[x][y];
			mtem = Move[x][y];
			if(mtem == -1) break;
			x = x + dir[mtem][0];
			y = y + dir[mtem][1];
		}

		while(cnt&&ans[cnt - 1] == '0') //删除前导〇
			cnt--;

		if(!cnt)
		{
			printf( "0\n" );
			continue;
		}
		for(int i = cnt - 1; i >= 0; i--)
		{
			printf( "%c", ans[i] );
		}
		printf( "\n" );
	}
	return 0;
}

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

时间: 2024-07-31 08:30:59

解题报告 之 HDU5335 Walk Out的相关文章

洛谷OJ P1126 机器人搬重物 解题报告

洛谷OJ P1126 机器人搬重物 解题报告 by MedalPluS [题目描述]    机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方.机器人接受的指令有:向前移动1步(Creep):向前移动2步(Walk):向前移动3步(Run):向左转(Left):向右转(Right).

USACO Section2.2 Party Lamps 解题报告 【icedream61】

lamps解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] N个灯,编号1~N.有4个开关,和C次改变某个开关状态的机会,试问最终所有灯的亮灭情况可能有哪些? 一号开关:改变所有灯的状态. 二号开关:改变所有奇数号灯的状态. 三号开关:改变所有

解题报告 之 POJ2135 Farm Tour

解题报告 之 POJ2135 Farm Tour Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn.

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

[noip2011]铺地毯(carpet)解题报告

最近在写noip2011的题,备战noip,先给自己加个油! 下面是noip2011的试题和自己的解题报告,希望对大家有帮助,题目1如下 1.铺地毯(carpet.cpp/c/pas) [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上.地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N