18276 走迷宫
时间限制:1000MS 代码长度限制:10KB
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC;VC
Description
有一个N*M的格子迷宫,1代表该格子为墙,不能通过,0代表可以通过,另外,在迷宫中 有一些传送门,走到传送门的入口即会自动被传送到传送门的出口(一次传送算1步)。人在迷宫中可以尝试 上下左右四个方向移动。现在给定一个迷宫和所有传送门的出入口,以及起点和终点, 问最少多少步可以走出迷宫。如果不能走出迷宫输出“die”。
输入格式
该程序为多CASE,第1行为CASE的数量 每一个CASE,第1行为两个数N(行)和M(列) 然后N行每行M个数 之后是一个数W,为传送门的数量 之后每行一个传送门的入口坐标c1(行),r1(列)和出口坐标c2,r2 之后是起点坐标和终点坐标sc(行) sr(列) ec(行) er(列) 注:传送门出入口和起点坐标和终点坐标不会出现在墙的位置 所有数字不超过100
输出格式
如题
输入样例
2 4 3 011 011 110 110 1 1 0 2 2 0 0 3 2 2 2 01 10 0 0 0 1 1
输出样例
3 die 之前处理传送的时候理解不对,那个传送如果用bfs是不能连续传的,但是用dfs就可以
#include<iostream>
#include<algorithm>
#include<sstream>
#include<math.h>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<string>
#include<stdio.h>
#include<ctype.h>
#include<cstring>
#include<cstdlib>
#include<iomanip>
using namespace std;
typedef long long ll;
int grid[105][105];
int via[105][105];
int mx[4] = { 1,0,0,-1 };
int my[4] = { 0,1,-1,0 };
int n, m;
struct point
{
int x, y;
};
int hh[105][105];
vector<point>cur;
int flag = 0;
point get_next(int x,int y)
{
point p;
p.x = x;
p.y = y;
if(hh[p.x][p.y]!=-1)
{
flag = 1;
grid[cur[hh[p.x][p.y]].x][cur[hh[p.x][p.y]].y] = grid[p.x][p.y] + 1;
int prex = p.x;
p.x = cur[hh[p.x][p.y]].x;
p.y = cur[hh[prex][p.y]].y;
}
return p;
}
int bfs(int bx,int by,int ex,int ey)
{
list<point>L;
point p;
p.x = bx;
p.y = by;
L.push_back(p);
while (L.size())
{
p = L.front();
p = get_next(p.x, p.y);
L.pop_front();
if (flag)
{
L.push_back(p), flag = 0;
continue;
}
if (p.x == ex && p.y == ey) return grid[ex][ey];
int pre_x = p.x;
int pre_y = p.y;
for (int i = 0; i < 4; i++)
{
int x = pre_x + mx[i];
int y = pre_y + my[i];
if (grid[x][y] || x < 0 || y < 0 || x >= n || y >= m) continue;
p.x = x;
p.y = y;
grid[x][y] = grid[pre_x][pre_y] + 1;
L.push_back(p);
}
}
return -1;
}
int main()
{
//freopen("in.txt", "r", stdin);
//ios::sync_with_stdio(0);
int T = 0;
cin >> T;
while (T--)
{
cin >> n >> m;
//getchar();
cur.clear();
fill_n(&grid[0][0], 105 * 105, 1);
fill_n(&via[0][0], 105 * 105, 0);
for (int i = 0; i < 105; i++)
{
for (int j = 0; j < 105; j++)
{
hh[i][j] = -1;
}
}
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int j = 0; j < m; j++)
{
grid[i][j]=s[j]-'0';
}
//getchar();
}
int w;
cin >> w;
for (int i = 0; i < w; i++)
{
int x, y,x1,y1;
cin >> x >> y>>x1>>y1;
point p;
p.x = x1;
p.y = y1;
cur.push_back(p);
hh[x][y]= cur.size() - 1;
}
int bx, by, ex, ey;
cin >> bx >> by >> ex >> ey;
int ans = bfs(bx, by, ex, ey);
if (ans == -1)
{
cout << "die\n";
}
else
{
cout << ans << "\n";
}
}
}