HDOJ 1026 Ignatius and the Princess I 解题报告

HDOJ 1026 Ignatius and the Princess I 解题报告

题意:

就是有一个迷宫还是地图的什么鬼东西,需要从左上角去往右上角,.表示可以走,X表示不可以走,然后如果出现数字n(1-9) 表示这个有一只小怪兽,你需要使用n秒钟来消灭它,然后问在这样的情况下至少需要多少秒才能出去,或者干脆出不去让上帝帮忙(两点不可达)。。。。。。

解法:

用普通的bfs即可,不过呢要稍微变一下下,假设数字都没有,那是最好的,如果有数字一旦走到这里就需要耽搁一定的时间。

我们可以这样想,一旦从队首却出来取出来的坐标点代表的是数字,那么我们入队列的时候,x和y不变,step(步数或者叫做秒数)增加,同时我们把这个数字减少,当这个数字变成0的时候,就等同于.,所以一旦从队首取出来的坐标点是.或者是0,那么就按照标准BFS的4个方向扫描,并确定入队关系。打印路径的时候,我为了方便先递归找到路径,然后用另一个函数格式化输出。千万不要遇到数字就在队列中连续插入一堆坐标,即使是遇到数字9,也要一次一次慢慢的通过队列自然的循环插入这个坐标

样例1 动画演示:



相同颜色代表同一时间

代码:

PPS:之所以有两种代码,是因为不知道为什么,Java代码最开始path是一个数组,死活过不了,后来改成Vector才过了,我的天,不知道怎么办呢?所以我用了一下C++。两份代码思路完全一模一样,都是AC代码

C++版本:

///*************************************
///*      IDE:CodeBlocks 16.01
///* Complier:mingw32-g++ C++11
///*   Author:gscsdlz
///*     Time:2016-08-06-11.28
///*************************************
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <list>
#include <map>
#include <set>

#define ULL unsigned long long
#define PI 3.1415926535
#define INF 0x3f3f3f3f
#define LL long long
#define eps 1e-8

using namespace std;

struct Node
{
    int x, y;
    int step;
    int id;
    int pre;
    bool flag;
    Node(int x, int y, int id, int step, int pre, bool flag)
    {
        this->x = x;
        this->y = y;
        this->id = id;
        this->step = step;
        this->pre = pre;
        this->flag = flag;
    }
};

int m, n;
const int maxn = 100 + 5;
char maze [maxn][maxn];
bool vis[maxn][maxn];
vector<Node>path;
int dir[][2]  = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}};
vector<Node>ans;

void getAns(int k)
{
    if(path[k].pre != -1)
        getAns(path[k].pre);
    ans.push_back(path[k]);
}

void print()
{
    for(int i = 0; i < ans.size() - 1; ++i)
    {
        if(ans[i].flag == true)
        {
            cout << ans[i].step << "s:FIGHT AT (" << ans[i].x << "," << ans[i].y << ")" << endl;;
        }
        if(ans[i + 1].flag == false)
        {
            cout << (ans[i].step + 1) << "s:";
            cout << "(" << ans[i].x << "," << ans[i].y << ")->";
            cout << "(" << ans[i + 1].x << "," << ans[i + 1].y << ")" << endl;;
        }
    }
    if(ans.rbegin()->flag == true)
    {
        cout << ans.rbegin()->step << "s:FIGHT AT (" << ans.rbegin()->x << "," << ans.rbegin()->y << ")" << endl;
    }
}

void bfs()
{
    queue<Node> q;
    q.push(Node(0, 0, 0, 0, -1, false));
    vis[0][0] = true;
    path.clear();
    path.push_back(Node(0, 0, 0, 0, -1, false));
    while(!q.empty())
    {
        Node fr = q.front();
        q.pop();
        if((fr.x + 1) == n && (fr.y + 1) == m && (maze[fr.x][fr.y] == '.' || maze[fr.x][fr.y] == '0'))
        {

            cout << "It takes " << fr.step <<  " seconds to reach the target position, let me show you the way." << endl;
            ans.clear();
            getAns(fr.id);
            print();
            cout << "FINISH" << endl;
            return;
        }
        if('1' <= maze[fr.x][fr.y] && maze[fr.x][fr.y] <= '9')
        {
            q.push(Node(fr.x, fr.y, path.size(), fr.step + 1, fr.id, true));
            path.push_back(Node(fr.x, fr.y, path.size(), fr.step + 1, fr.id, true));
            maze[fr.x][fr.y]--;
            vis[fr.x][fr.y] = true;
        }
        else
        {
            for(int i = 0; i < 4; i++)
            {
                int nx = fr.x + dir[i][0];
                int ny = fr.y + dir[i][1];
                if(0 <= nx && nx < n && 0 <= ny && ny < m && vis[nx][ny] == false && maze[nx][ny] != 'X')
                {
                    q.push(Node(nx, ny, path.size(), fr.step + 1, fr.id, false));
                    path.push_back(Node(nx, ny, path.size(), fr.step + 1, fr.id, false));
                    vis[nx][ny] = true;
                }
            }
        }
    }
    cout << "God please help our poor hero." << endl;
    cout << "FINISH" << endl;
}

int main()
{
#ifdef LOCAL
    ///freopen("in.txt", "r", stdin);
    ///freopen("out.txt", "w", stdout);
#endif // LOCAL
    while(cin >> n >> m)
    {
        for(int i = 0; i < n; ++i)
        {

            for(int j = 0; j < m; ++j)
                cin >> maze[i][j];
        }
        memset(vis, false, sizeof(vis));
        bfs();
    }
    return 0;
}
Java版本:

import java.util.*;
import java.text.DecimalFormat;
import java.math.*;

class Node {
	int x, y;
	int step;
	int id;
	int pre;
	boolean flag;
	Node(int x, int y, int id, int step, int pre, boolean flag){
		this.x = x;
		this.y = y;
		this.id = id;
		this.step = step;
		this.pre = pre;
		this.flag = flag;
	}
	public String toString() {
		return id + " " + x + " " + y + " " +step + " "  + pre + " " + flag;
	}
}

public class Main {

	static int m, n;
	static final int maxn = 100 + 5;
	static char[][] maze = new char[maxn][maxn];
	static boolean[][] vis = new boolean[maxn][maxn];
	static int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
	static Vector<Node>ans = new Vector<Node>();
	static Vector<Node>path = new Vector<Node>();
	
	static void getAns(int k) {
		if(path.elementAt(k).pre != -1)
			getAns(path.elementAt(k).pre);
		ans.add(path.elementAt(k));
	}
	
	static void print() {
		for(int i = 0; i < ans.size() - 1; ++i) {
			if(ans.elementAt(i).flag == true) {
				System.out.println(ans.elementAt(i).step+"s:FIGHT AT ("+ans.elementAt(i).x+","+ans.elementAt(i).y+")");
			}
		    if(ans.elementAt(i + 1).flag == false) {
				System.out.print((ans.elementAt(i).step + 1) + "s:");
				System.out.print("(" + ans.elementAt(i).x + "," + ans.elementAt(i).y + ")->");
				System.out.println("(" + ans.elementAt(i + 1).x + "," + ans.elementAt(i + 1).y + ")");
		    }
		}
		if(ans.lastElement().flag == true) {
			System.out.println(ans.lastElement().step+"s:FIGHT AT ("+ans.lastElement().x+","+ans.lastElement().y+")");

		}
	}
	
	static void bfs() {
		Queue<Node> q = new LinkedList<Node>();
		q.add(new Node(0, 0, 0, 0, -1, false));
		vis[0][0] = true;
		path.add(new Node(0, 0, 0, 0, -1, false));
		while(q.isEmpty() == false) {
			Node fr = q.remove();
			if((fr.x + 1) == n && (fr.y + 1) == m && (maze[fr.x][fr.y] == '.' || maze[fr.x][fr.y] == '0')) {

				System.out.println("It takes " + fr.step +" seconds to reach the target position, let me show you the way.");
				ans.clear();
				getAns(fr.id);
				print();
				System.out.println("FINISH");
				return;
			}
			if('1' <= maze[fr.x][fr.y] && maze[fr.x][fr.y] <= '9') {
				q.add(new Node(fr.x, fr.y, path.size(), fr.step + 1, fr.id, true));
				path.add(new Node(fr.x, fr.y, path.size(), fr.step + 1, fr.id, true));
				maze[fr.x][fr.y]--;
				vis[fr.x][fr.y] = true;
			} else {
				for(int i = 0; i < 4; i++) {
					int nx = fr.x + dir[i][0];
					int ny = fr.y + dir[i][1];
					if(0 <= nx && nx < n && 0 <= ny && ny < m && vis[nx][ny] == false && maze[nx][ny] != 'X') {
							q.add(new Node(nx, ny, path.size(), fr.step + 1, fr.id, false));
							path.add(new Node(nx, ny, path.size(), fr.step + 1, fr.id, false));
							vis[nx][ny] = true;
					}
				}
			}
		}
		System.out.println("God please help our poor hero.");
		System.out.println("FINISH");
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNextInt()) {
			n = cin.nextInt();
			m = cin.nextInt();
			
			String tmp;
			for(int i = 0; i < n; ++i) {
				tmp = cin.next();
				for(int j = 0; j < m; ++j)
					maze[i][j] = tmp.charAt(j);
			}
			for(int i = 0; i < maxn; ++i)
				Arrays.fill(vis[i], false);
			bfs();
		}
		cin.close();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gscsdlz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值