当前位置:网站首页>POJ Project Summer
POJ Project Summer
2022-06-30 16:38:00 【leehyukshuai】
04E:Project Summer
Total time limit :
1000ms
Memory limit :
65536kB
describe
Small I And small B Recently, I became addicted to a product called 《Project Summer》 The game of , Small I Play the innocent who need to escape in this game (Innocent), Small B Play this game to catch the innocent , The traitor who prevented his escape (Betrayer).
The map of this game is a N That's ok M Column The rectangular , Each grid point represents a position . '#' Represent obstacles in the map ,'.' Represents the open space in the map , Besides , There are also... In the map Only the Betrayer To use the portal , In small letters 'a' - 'z' Mark , They appear in pairs on the map .
Roles can cost 1 The unit time goes from one grid to the next 4 Another cell in a clearing ( Don't walk out of the map boundary or onto obstacles ).
Besides , When small B When the Betrayer comes to a portal , He can spend 1 The unit time is transferred from the current grid to another portal with the same letter as the current grid ( He can also choose not to transmit , It doesn't take any time , Stay where you are ). Transfer yes two-way Of . such as , Now small B Go to the mark as 'a' On the grid , Then he can choose to spend one unit of time to transfer to another marked 'a' On the grid , You can also choose not to transmit , Then he just stays where he is .
Now? , Small I Be small B The trap of , Cannot move . Give the small on the map B And small I In the grid ( They are all standing in the open space ), Seek small B At least how much time will it take to get there I Catch him in the grid . If small I Unable to grasp the small B, Output -1
Input
One number in the first line T, Represents the number of data groups .
Next, describe T Group data , Each set of data starts with two positive integers N, M Indicates that the map is N That's ok M Column rectangle .
Next N That's ok , Each row M Characters , Represents a map . On the map , use '.' Representing vacant land ,'#' It means an obstacle ,'a'-'z' Indicates the portal ,'B' It means small B Initial position ,'I' It means small I Initial position .
For each set of data , Make sure that the same portal appears exactly twice on the map .
T,N,M <= 100
Output
T That's ok , The first i Line output 'Case #i: t', It means the first one i The answer to the set of data is t. Small B Minimum need t Unit time to go small I In the grid . If small I Unable to grasp the small B, Output -1
The sample input
3
5 5
Bx#..
#a.#.
.....
##..#
.x.aI
5 5
BIa.a
x#.x.
.#.##
.....
#####
2 2
B#
#ISample output
Case #1: 4
Case #2: 1
Case #3: -1Tips
For the first set of data , Suppose the row is labeled from top to bottom 1 To 5, Columns are labeled from left to right 1 To 5, Small B The beginning is (1, 1). Small B The best route is :
(1, 1) -> (1, 2) -> (2, 2) -> (5, 4) -> (5, 5). That is, go to the place marked with x The portal is ignored when the portal , Go to marked as a When using the portal of .
For the second set of data , Small B Direct costs 1 If you go one square to the right in unit time, you can catch the small I, Therefore, the output 1.
For the third set of data , Small B Can't walk to the small I Where you are , Therefore, the output -1.
answer
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int T, N, M;
int ii, jj;
int directs[4][2]{
{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char a[100][100];
int visited[100][100];
struct Data
{
int i, j, t;
Data(int _i, int _j, int _t) : i(_i), j(_j), t(_t) {}
};
Data gogogo(const Data &d)
{
char c = a[d.i][d.j];
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
if (a[i][j] == c && !(d.i == i && d.j == j))
{
return Data(i, j, d.t + 1);
}
}
}
return d;
}
int main()
{
cin >> T;
for (int t = 1; t <= T; ++t)
{
cin >> N >> M;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
cin >> a[i][j];
if (a[i][j] == 'B')
{
ii = i;
jj = j;
}
}
}
queue<Data> q;
memset(visited, -1, sizeof(visited));
q.push(Data(ii, jj, 0));
visited[ii][jj] = 0;
bool flag = 0;
while (!q.empty())
{
Data cur = q.front();
if (a[cur.i][cur.j] == 'I')
{
flag = 1;
break;
}
q.pop();
int ni, nj, nt;
for (int d = 0; d < 4; ++d)
{
ni = cur.i + directs[d][0];
nj = cur.j + directs[d][1];
nt = cur.t + 1;
if (ni >= 0 && ni < N && nj < M && nj >= 0 && a[ni][nj] != '#' && (visited[ni][nj] == -1 || nt < visited[ni][nj]))
{
visited[ni][nj] = nt;
q.push(Data(ni, nj, nt));
}
}
// gas !isalpha Function in OJ Will be judged wrong
if (a[cur.i][cur.j] <= 'z' && a[cur.i][cur.j] >= 'a')
{
Data nd = gogogo(cur);
if (visited[nd.i][nd.j] == -1 || nd.t < visited[nd.i][nd.j])
{
visited[nd.i][nd.j] = nd.t;
q.push(nd);
}
}
}
cout << "Case #" << t << ": " << ((flag) ? q.front().t : -1) << endl;
}
}边栏推荐
- How to connect the Internet Reading Notes - Summary
- Headhunter 50, 000, I'll go to VC
- Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
- Three development trends of enterprise application viewed from the third technological revolution
- 快照和备份
- 互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
- 【Verilog基础】十进制负数的八进制、十六进制表示
- 容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
- Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
- Open source STM32 USB-CAN project
猜你喜欢
MySQL8.0开启远程连接权限的方法步骤

构建适合组织的云原生可观测性能力

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”

19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project

Build cloud native observability capability suitable for organizations

牛客网:最小花费爬楼梯

CloudXR如何推动XR的未来发展

Implementation of Devops in the core field of qunar, the Internet R & D Efficiency

“低代码”在企业数字化转型中扮演着什么角色?

中航无人机科创板上市:市值385亿 拳头产品是翼龙无人机
随机推荐
Does flinkcdc have to be a clustered version if the monitored database is Mongo
MySQL master-slave configuration
Headhunter 50, 000, I'll go to VC
【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
MC Instruction Decoder
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
How to get the preferential activities for stock account opening? Is online account opening safe?
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键
有意思的鼠标指针交互探究
备战数学建模36-时间序列模型2
Cloud XR, how to help industrial upgrading
Arcmap操作系列:80平面转经纬度84
招标公告:深圳市财政局数据库异地灾备项目
MySQL8.0开启远程连接权限的方法步骤
Dart: string replace related methods to solve replacement characters
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
Mysql代理中间件Atlas安装和配置
大学生研究生毕业找工作,该选择哪个方向?