穿越雷区

X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。

某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?

已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。 例如:

A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

坦克车只能水平或垂直方向上移动到相邻的区。

数据格式要求:

输入第一行是一个整数n,表示方阵的大小, 4<=n<100

接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。

A,B都只出现一次。

要求输出一个整数,表示坦克从A区到B区的最少移动步数。

如果没有方案,则输出-1

例如:

用户输入:

5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

则程序应该输出:

10

资源约定:

峰值内存消耗 < 512M

CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

注意: main函数需要返回0

注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。

注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。

提交时,注意选择所期望的编译器类型。

bfs求最短路径。

代码:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

typedef pair<int,int> pa;
typedef pair<pa,int> paa;
int n;
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
char s[100][100];
bool vis[100][100];
int sx,sy;
int main() {
    scanf("%d",&n);
    for(int i = 0;i < n;i ++) {
        for(int j = 0;j < n;j ++) {
            getchar();
            s[i][j] = getchar();
            if(s[i][j] == 'A') {
                sx = i;
                sy = j;
            }
        }
    }
    queue<paa> q;
    q.push(paa(pa(sx,sy),0));
    vis[sx][sy] = true;
    int ans = -1;
    while(!q.empty()) {
        int x = q.front().first.first;
        int y = q.front().first.second;
        int num = q.front().second;
        if(s[x][y] == 'B') ans = num;
        q.pop();
        for(int i = 0;i < 4;i ++) {
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(tx < 0 || ty < 0 || tx >= n || ty >= n || vis[tx][ty] || s[tx][ty] == s[x][y]) continue;
            vis[tx][ty] = true;
            q.push(paa(pa(tx,ty),num + 1));
        }
    }
    printf("%d",ans);
}
原文地址:https://www.cnblogs.com/8023spz/p/10686903.html