HDU 2102 A计划 (BFS)
2024-08-30 08:18:03
A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15463 Accepted Submission(s): 3859
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES
Source
其实很简单的: 有两个地图, 如果遇到#就到第二个地图去搜, 再次遇到#就再回到第一个地图, node里面多一个flag标记是在第几个地图 .
开始没A是因为题面坑:
1没说清是必须在T时刻遇到公主还是<=T都可以
2没说还有两个图同时都是#的情况,因为从mat1跳到mat2是不消耗时间的,所以就搞不清到底是怎样?直接GG?还是只跳一次?
#include<queue>
#include<math.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream> using namespace std;
#define N 112345678
#define M 155
#define INF 0x3f3f3f3f
struct node
{
int x,y,time;
bool flag;
}p,st; int n, m, a, b, x, y, t, cnt, ans;
int dx[] = {, , , -};
int dy[] = {, -, , };
char mat1[M][M],mat2[M][M];
bool v1[M][M],v2[M][M]; void Bfs(int x, int y , int time)
{
ans = -;
memset(v1,,sizeof(v1));
memset(v2,,sizeof(v2));
queue<node>q;
while(!q.empty()) q.pop(); st.x = x, st.y = y, st.time = time; st.flag = true;
q.push(st);
v1[st.x][st.y] = true;
while(!q.empty())
{
st = q.front();
//printf("front = %d %d time = %d, flag = %d\n",st.x, st.y, st.time, st.flag);
q.pop();
for(int i = ; i < ; i++)
{
node next = st;
next.x += dx[i], next.y += dy[i], next.time++;
if(next.flag == true)
{
if(mat1[next.x][next.y] == 'P')
{
ans = next.time ;
return;
}
if(next.x < || next.x >= n || next.y < || next.y >=m)continue;
if(mat1[next.x][next.y] == '*' || mat1[next.x][next.y] == '#'&&mat2[next.x][next.y] == '*' || v1[next.x][next.y] == true || mat1[next.x][next.y] == '#'&&mat2[next.x][next.y] == '#') continue;
if(mat1[next.x][next.y] == '#' )
{
next.flag = !next.flag;
}
v1[next.x][next.y] = true;
}
if(next.flag == false)
{
if(mat2[next.x][next.y] == 'P')
{
ans = next.time ;
return;
}
if(next.x < || next.x >= n || next.y < || next.y >=m)continue;
if(mat2[next.x][next.y] == '*' || mat2[next.x][next.y] == '#'&&mat1[next.x][next.y] == '*' || v2[next.x][next.y] == true || mat1[next.x][next.y] == '#'&&mat2[next.x][next.y] == '#') continue;
if(mat2[next.x][next.y] == '#' )
{
next.flag = !next.flag;
}
v2[next.x][next.y] = true;
}
q.push(next);
}
}
} int main()
{
int T;cin>>T;
while(T--)
{
cin>>n>>m>>t;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
scanf(" %c", &mat1[i][j]);
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
scanf(" %c", &mat2[i][j]); Bfs(,,);
if(ans == - || ans > t)
cout<<"NO"<<endl;
else cout<<"YES"<<endl; }
return ;
}
最新文章
- loadrunner中lr_save_searched_string函数的使用
- nav元素
- 各个JSON技术的比较
- 第三章 设计程序架构 之 设计实现Windows Azure 角色生命周期
- jquery.fullCalendar官方文档翻译(一款小巧好用的日程管理日历, 可集成Google Calendar)
- PDB符号文件信息
- windows mongodb 安装
- ORACLE存储过程笔记2
- 使用netty的第一个Hello World
- Linux 使用nmcli配置网络
- Maven使用lib下的包
- 05python下
- Android性能优化系列之Bitmap图片优化
- Linux基础-yum软件包管理
- Dream------scala--scala内部类实战
- 使用auth_request模块实现nginx端鉴权控制
- 第七周 ch04 课下测试补交
- win32之取画刷的方法
- Nginx自定义模块编写:根据post参数路由到不同服务器
- Bootstrap 模态框(Modal)插件数据传值