hdu 1072 解题报告 ---- Nightmare
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1072
这题是一个BFS的题目,是对于每个结点进行四个方向的循环遍历行走。
要点:如果该结点被第二次访问,则需判断该点前次标记的剩余时间是否小于当前进入后时间,若不是则不走该点,防止无限循环。
画模拟图可知其行走形式是从起点向外呈放射形的,因此可以保证不会走重复,也就可以保证他走过的点,标记之后都是最优解。
C++版:
#include<stdio.h>
#include<string.h>
struct Node{
int x,y,step,time;
} ;
const int MAX =10 ;
int n,m ;
int map[MAX][MAX] ;
int vis[MAX][MAX] ;
int sx,sy,ex,ey ;
const int time = 6 ;
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}} ;
Node que[MAX*MAX] ;
int ok(int x,int y)
{
if(x >=0 && x< n && y>=0 && y< m)
return 1;
return 0;
}
int bfs(int sx ,int sy)
{
int head = 0 ,tail=1 ;
que[head].x = sx ;
que[head].y = sy ;
que[head].step = 0 ;
que[head].time = time ;
Node tmp ;
while(head < tail)
{
tmp = que[head] ;
if(tmp.x == ex && tmp.y == ey && tmp.time >0)
{
return tmp.step ;
}
for(int i = 0 ; i < 4 ;i++)
{
int k = tmp.x +d[i][0] , v = tmp.y+d[i][1] ;
if(ok(k,v) && map[k][v] && tmp.time - 1 > vis[k][v])
{
que[tail].x = k ;
que[tail].y = v ;
que[tail].step = tmp.step+1 ;
que[tail].time = tmp.time-1 ;
if(map[k][v] == 4)
que[tail].time = time ;
vis[k][v] = que[tail].time ;
tail++ ;
}
}
head++ ;
}
return -1 ;
}
int main()
{
int t;
scanf("%d",&t) ;
while(t--)
{
scanf("%d %d",&n,&m) ;
for(int i = 0 ; i<n;i++)
{
for(int j = 0 ; j < m ;j++)
{
scanf("%d" , &map[i][j]) ;
if(map[i][j] == 2) sx = i,sy =j,map[i][j] = 1 ;
if(map[i][j] == 3) ex = i,ey =j,map[i][j] = 1 ;
}
}
memset(vis,0,sizeof(vis)) ;
vis[sx][sy] = 6;
printf("%d\n",bfs(sx,sy)) ;
}
return 0 ;
}
Java版:
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
static Scanner scanner = new Scanner(System.in);
static final int TIME = 6;
static int len = 0, high = 0;;// 答案,迷宫的行数,列数
static int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };// 右、下、左、上
static Point start, end;// 起点终点
static PointInfo[][] map;// map[i][j],图中每个结点信息
static Queue<Point> que = new LinkedBlockingDeque<Point>();// 保存要遍历的结点
static {
// 初始化对象
start = new Point();
end = new Point();
map = new PointInfo[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
map[i][j] = new PointInfo();
}
}
public static void main(String[] args) {
int n = scanner.nextInt();
while (n-- > 0) {
// 接受数据
if (!scanner.hasNextInt())
return;
len = scanner.nextInt();
high = scanner.nextInt();
que.clear();
for (int i = 0; i < len; i++) {
for (int j = 0; j < high; j++) {
map[i][j].type = scanner.nextInt();
map[i][j].time = 0;
if (map[i][j].type == 2) {
start.x = i;
start.y = j;
start.time = TIME;
map[i][j].type = 1;// 设为空白
} else if (map[i][j].type == 3) {
end.x = i;
end.y = j;
end.time = 0;
map[i][j].type = 1;
}
}
}
// 执行操作,输出结果
System.out.println(bfs());// 广搜
}
}
public static int bfs() {
que.offer(start);
while (!que.isEmpty()) {
Point from = que.poll();
if (from.x == end.x && from.y == end.y && from.time > 0)
return from.step;
for (int i = 0; i < 4; i++) {
Point to = new Point();
to.x = from.x + move[i][0];
to.y = from.y + move[i][1];
to.step = from.step + 1;
to.time = from.time - 1;
if (to.x < 0 || to.y < 0 || to.x >= len || to.y >= high)//越界
continue;
if (map[to.x][to.y].type != 0 && map[to.x][to.y].time < to.time) {//若不是障碍,且原剩余时间小于当前的
if (map[to.x][to.y].type == 4)// 重置炸弹时间
to.time = TIME;
map[to.x][to.y].time = to.time;
que.offer(to);
}
}
}
return -1;
}
}
class Point {
int x, y, time, step;
}
class PointInfo {
int type;// 位置类型
int time;// 剩余时间
}