至于题意:
已经说的很清楚了:
我又回来了 | ||||||
| ||||||
Description | ||||||
小辉穿越到了一个很适合他的年代,22222年,他发现虽然他还在地球上,但是这个世界早已不同,经过了世界大破灭,天地大变,已经到了能够修真的年代,他也加入了一个宗门,少林寺。。。一天,小辉想去找小尼姑们一起快乐地玩耍,他知道这个世界存在着传送门,在一瞬间就可以从一个地方传送到另外一个有传送门的地方,他的老毛病(讨厌走路)又犯了,他决定到哪个小尼姑那里的步数最少,就去找哪个小尼姑。 首先给出小辉和小尼姑所在的N*N平面图,如图为6*6平面图。 ....#. .*.#.. ..@... ###### .@.... ...... 有Q个小尼姑,每个小尼姑所在的地点用坐标表示,左上角处坐标为(0,0)。 图中'*'代表小辉所在的位置,图中即(1,1),'.'代表空地,'#'代表不能直接通过的建筑物,'@'代表传送门,传送门不是不能通过的建筑物。 小辉将去找他所能到达的并且离他步数最少的那个小尼姑。 小尼姑的位置可能在除建筑物的任意位置上。 | ||||||
Input | ||||||
有多组测试数据,处理到文件结束。 对于每组测试数据,第一行是两个整数N(2<=N<=100),Q(1<=Q<=1000),分别代表地区的边长和小尼姑的个数. 接下来输入n*n平面图,代表地区平面图。 然后Q行,每行一个坐标代表小尼姑所在位置。 | ||||||
Output | ||||||
输出小辉到那个小尼姑的步数,如果没有满足条件的小尼姑,则输出cry,最后换行. | ||||||
Sample Input | ||||||
6 3 ....#. .*.#.. ..@... ###### .@.... .....@ 0 5 1 4 4 1 3 2 *@. ### ..@ 0 2 2 1 | ||||||
Sample Output | ||||||
2 2 | ||||||
Source | ||||||
新生练习赛(2013.11.17) | ||||||
Author | ||||||
hrbust |
一开始用最短路算法做的,但是显示编译错误,根本没找出哪儿错了,然后闲着没事儿 用搜索写了写,过了,然后把最短路代码改了一下,交上也a了。
bfs版本:
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-09-07 21:12
#
# Filename: hdu1015.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int s, e;
int n, q;
int dir[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int vis[103][203];
char G[103][103];
typedef pair<int,int>PLL;
set<PLL>dic;
struct Node
{
int x, y, step;
Node(int x,int y, int step):x(x),y(y),step(step){}
bool operator < (const Node &a) const{
return step > a.step;
}
};
bool check(int x, int y){
if(x >= 0 && x < n && y >= 0 && y < n && !vis[x][y] && G[x][y] != '#'){
return 1;
}
else return 0;
}
bool bfs()
{
memset(vis, 0, sizeof(vis));
vis[s][e] = 1;
priority_queue<Node>qq;
qq.push(Node(s, e, 0));
while(!qq.empty()){
Node u = qq.top();
qq.pop();
if(dic.count(make_pair(u.x, u.y))){
cout<<u.step<<endl;return 1 ;
}
for(int i = 0; i <4 ;i++){
int tx = u.x + dir[i][0];
int ty = u.y + dir[i][1];//cout<<"u . x = "<<u.x<<" u.y = "<<u.y<<" "<<u.step<<endl;
if(check(tx, ty)){
if(G[tx][ty] == '@'){
for(int t = 0; t <n ;t++){
for(int c = 0; c <n; c++){
if(G[t][c] == '@'){//cout<<"t = "<<t<<"c = "<<c<<" step = "<<u.step + 1<<endl;
qq.push(Node(t, c, u.step + 1));
vis[t][c] = 1;
}
}
}
}
else
{//cout<<"tx = "<<tx<<" ty = "<<ty<<" "<<u.step + 1<<endl;
qq.push(Node(tx, ty, u.step + 1));
vis[tx][ty] = 1;
}
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&n, &q) != EOF)
{
dic.clear();
memset(G, '\0', sizeof(G));
for(int i = 0; i < n ;i++){
scanf("%s",G[i]);
for(int j =0; j < n ; j++){
if(G[i][j] == '*'){
s = i;e = j;break;
}
}
}
for(int i = 0; i < q; i++){
int x, y;
scanf("%d%d",&x, &y);
dic.insert(make_pair(x, y));
}
if(dic.count(make_pair(s, e))){
cout<<0<<endl;continue;
}
if(!bfs()){
cout<<"cry"<<endl;
}
}
return 0;
}
最短路版本:
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-09-07 21:12
#
# Filename: hdu1015.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int s, e;
int n, q;
int dir[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int vis[103][103];
char G[103][103];
int d[103][102];
typedef pair<int,int> PLL;
set<PLL>dic;
struct Node
{
int x, y, step;
Node(){}
Node(int x, int y, int step):x(x),y(y),step(step){}
bool friend operator <(Node a, Node b){
return a. step > b.step;
}
}node[1003];
bool bfs()
{
memset(vis, 0, sizeof(vis));
memset(d, 0x3f3f3f3f, sizeof(d));
d[s][e] = 0;
vis[s][e] = 1;
priority_queue<Node>qq;
qq.push(Node(s, e, 0));
int flag = 0;
int coun = 0;
while(!qq.empty()){
Node u = qq.top();
qq.pop();
if(dic.count(make_pair(u.x, u.y))){
cout<<u.step<<endl;return 1 ;
}
vis[u.x][u.y] = 1;
for(int i = 0; i < 4; i++){
int tx = u.x + dir[i][0];
int ty = u.y + dir[i][1];
if(!vis[tx][ty] && u.step + 1 < d[tx][ty] && tx >= 0 && tx < n && ty >= 0 && ty < n && G[tx][ty] != '#'){
if(G[tx][ty] == '@' && flag == 0){
flag = 1;
for(int t = 0; t < n; t++){
for(int c = 0; c < n ;c++){
if(G[t][c] == '@'){
qq.push(Node(t, c,d[t][c] = u.step + 1));
}
}
}
}
else
qq.push(Node(tx, ty,d[tx][ty] = u.step + 1));
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&n, &q) != EOF)
{
int flag = 0;
for(int i = 0; i < n ;i++){
scanf("%s",G[i]);
for(int j = 0; j < n ;j ++){
if(G[i][j] == '*' && flag == 0){
s = i;e = j;flag = 1;
}
}
}
dic.clear();
for(int i = 0; i < q; i++){
int x, y;
scanf("%d%d",&x, &y);
dic.insert(make_pair(x, y));
}
if(dic.count(make_pair(s, e))){
cout<<0<<endl;continue;
}
if(!bfs()){
cout<<"cry"<<endl;
}
}
return 0;
}
/*
6 1
....#.
.*.#..
..@#..
######
.@....
.....@
1 5
3 2
*@.
###
..@
0 2
1 1
*/