首页 > ACM题库 > HDU-杭电 > HDU 1364 Illusive Chase-最短路径-[解题报告] C++
2013
12-09

HDU 1364 Illusive Chase-最短路径-[解题报告] C++

Illusive Chase

问题描述 :

Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audience of youngsters, placed around an m n field. Tom which is turned off initially is placed in some arbitrary point in the field by a volunteer from the audience. At time zero of the show, Tom is turned on by a remote control. Poor Tom is shown a holographic illusion of Jerry in a short distance such that a direct path between them is either vertical or horizontal. There may be obstacles in the field, but the illusion is always placed such that in the direct path between Tom and the illusion, there would be no obstacles. Tom tries to reach Jerry, but as soon as he gets there, the illusion changes its place and the chase goes on. Let’s call each chase in one direction (up, down, left, and right), a chase trip. Each trip starts from where the last illusion was deemed and ends where the next illusion is deemed out. After a number of chase trips, the holographic illusion no more shows up, and poor Tom wonders what to do next. At this time, he is signaled that for sure, if he returns to where he started the chase, a real Jerry is sleeping and he can catch it.

To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal or vertical. It’s assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.

The problem is that Tom’s logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom’s memory to help him go back. But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.

输入:

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers m and n, which are the number of rows and columns of the grid respectively (1 <= m, n <= 100). Next, there are m lines, each containing n integers which are either 0 or 1, indicating whether the corresponding cell of the grid is empty (0) or occupied by an obstacle (1). After description of the field, there is a sequence of lines, each corresponding to a chase trip of Tom (in order). Each line contains two positive integers which together specify the range of steps Tom has taken (inclusive), followed by a single upper-case character indicating the direction of the chase trip, which is one of the four cases of R (for right), L (for left), U (for up), and D (for down). (Note that these directions are relative to the field and are not directions to which Tom turns). This part of the test case is terminated by a line containing exactly two zeros.

输出:

For each test case, there should be a single line, containing an integer indicating the number of cells that Tom might have started the chase from.

样例输入:

2
6 6
0 0 0 0 0 0
0 0 0 1 1 0
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 1 0 1
0 0 0 0 0 1
1 2 R
1 2 D
1 1 R
0 0
3 4
0 0 0 0
0 0 0 0
0 0 0 0
1 2 R
3 7 U
0 0

样例输出:

10
0

这题目应该算是比较基础的差分约束吧,嘻嘻,也算是我的第一道差分约束,理解了题意之后,就是转化和构图的问题了

<[if gte mso 9]>

Normal
0

7.8 磅
0
2

false
false
false














MicrosoftInternetExplorer4

<[if gte mso 9]>


<[if gte mso 10]>

构图方法:

首先进行转换:a[j]+…+a[j+m] = a[1]+…a[j+m] – (a[1]+…+a[j-1]) = sum[j+m] –

sum[j-1]
>(<) ki.
差分约束只能全部是<=或者(>=).

第二步转换: sum[j+m]-sum[j-1] <= ki-1 或者
sum[j-1]-sum[j+m] <= -ki-1.

约束图构造好后就是简单的Bellman-Ford!

注意,用bellman-Ford一定<= 或者>=,而题目给的只有< 或者> ,所以为了能用Bellman-Ford解决,就得在权重上面动手脚了,如上所示。

#include<iostream>
#include<string>
#include<queue>
#define MAXINT 9999999
#define MAXN 110
using namespace std;
int dis[MAXN],n,m,min1,max1;
struct edge
{
	int u,v,w;
}e[MAXN];
bool bellman_ford()
{
	for(int i=min1;i<=max1;i++)
		dis[i]=MAXINT;
	dis[min1]=0;//这里可要也可不要,因为Bellman-Ford是对所有边进行操作的,即使没有源点也照样进行松弛操作
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(dis[e[j].v]>dis[e[j].u]+e[j].w)
				dis[e[j].v]=dis[e[j].u]+e[j].w;
		}
	}
		for(int j=0;j<m;j++)
		{
			if(dis[e[j].v]>dis[e[j].u]+e[j].w)
				return false;
		}
		return true;
}
int main()
{
	char str[5];
	int a,b,c;
	while(cin>>n)
	{
		if(n==0) break;
		cin>>m;
		max1=0;min1=MAXN;
		for(int i=0;i<m;i++)
		{
			cin>>a>>b>>str>>c;
			max1=max(a+b+1,max1);
			min1=min(a,min1);
			min1=min(b,min1);
			if(strcmp(str,"gt")==0)
			{
				e[i].u=a+b+1;
				e[i].v=a;
				e[i].w=-c-1;
			}
			else {
				e[i].v=a+b+1;
				e[i].u=a;
				e[i].w=c-1;
			}
		}
		if(bellman_ford())
			cout<<"lamentable kingdom"<<endl;
		else cout<<"successful conspiracy"<<endl;
	}
	return 0;
}

解题报告转自:http://www.cnblogs.com/nanke/archive/2011/08/14/2137638.html


  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  2. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish