解题思路:
最普通的一道搜索题目,dfs跟bfs都能做,需要注意的是每一行的两个数据中间有空格。
注意事项:
参考代码:
#include <iostream>
#include <algorithm>
#include"stdio.h"
#include"math.h"
#include<vector>
#include<queue>
#include"cstring"
#include"set"
#include"map"
#include <memory.h>
#include<string>
#include<functional>
#include"time.h"
#define maxn 200001
using namespace std;
int n,step;
char ma[110][110];
int zx[4] = {0,0,-1,1};
int zy[4] = {2,-2,0,0};
int visit[110][110];
int mi = 99999;
void dfs(int x,int y,int step)
{
if(ma[x][y] == 'B')
{
mi = min(mi,step);
return;
}else{
for(int i = 0;i<4;i++)
{
int tx = x+zx[i];
int ty = y+zy[i];
if(tx>=0 && tx<n && ty>=0 && ty<=(n-1)*2 && visit[tx][ty] == 0 && ma[tx][ty]!=ma[x][y]){
visit[tx][ty] = 1;
step++;
dfs(tx,ty,step);
step--;
visit[tx][ty] = 0;
}
}
}
}
int main() {
int x,y;
cin >> n; cin.get();
for (int i = 0; i < n; i++) {
gets(ma[i]);
}
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
if(ma[i][j] == 'A')
{
x = i;
y = j;
}
/*for(int i = 0;i<n;i++)
{
puts(ma[i]);
}*/
//printf("%d %d \n",x,y);
dfs(x,y,step);
printf("%d\n",mi);
return 0;
}
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)浏览:473 |
母牛的故事 (C语言代码)浏览:1298 |
C语言程序设计教程(第三版)课后习题9.8 (Java代码)浏览:1637 |
c primer plus 第十二章 12.1小节浏览:377 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:603 |
WU-输入输出格式练习 (C++代码)浏览:1079 |
剪刀石头布 (C语言代码)浏览:751 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:262 |
字符逆序 (C语言代码)浏览:614 |
A+B for Input-Output Practice (C语言代码)浏览:463 |