正确代码:

 #include<iostream>
#include<queue>
#define N 210
#define inf 0xffffff
using namespace std;
int m,n,mark[N][N],dis[N][N][],dir[][]={,, ,, -,, ,-},flag;
char s[N][N];
struct node{
int x,y,step;
};
bool judge(int x,int y)
{
if(x>= && x<m && y>= && y<n && s[x][y]!='#' && mark[x][y]==)
return ;
return ;
}
void bfs(int x,int y)
{
int k;
queue<node>q;
node cur,next;
cur.x=x;cur.y=y;cur.step=;
mark[x][y]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
next.step=cur.step+;
for(k=;k<;k++)
{
next.x=x=cur.x+dir[k][];
next.y=y=cur.y+dir[k][];
if(judge(x,y))
{
mark[x][y]=;
if(s[x][y]=='@')
dis[x][y][flag]=next.step;
q.push(next);
}
}
}
} int main()
{
int i,j,min;
while(scanf("%d %d",&m,&n)!=-)
{
min=inf;
for(i=;i<m;i++)
for(j=;j<n;j++)
dis[i][j][]=dis[i][j][]=inf; for(i=;i<m;i++)
scanf("%s",s[i]);
for(i=;i<m;i++)
for(j=;j<n;j++)
{
if(s[i][j]=='Y')
{
flag=;
memset(mark,,sizeof(mark));
bfs(i,j);
}
else if(s[i][j]=='M')
{
flag=;
memset(mark,,sizeof(mark));
bfs(i,j);
}
}
for(i=;i<m;i++)
for(j=;j<n;j++)
if(s[i][j]=='@' && min>dis[i][j][]+dis[i][j][])
min=dis[i][j][]+dis[i][j][];
printf("%d\n",min*);
}
return ;
}

被逼疯代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int x;
int y;
int step;
};
char g[][];
int stepA[][];
int stepB[][];
queue<node>q;
int yx, yy, mx, my;
int x, y;
long long ans;
int d[][]={{,},{,},{,-},{-,}}; int main()
{
// freopen("in.in","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d%d",&x,&y)!=EOF)
{
memset(g,,sizeof(g));
memset(stepA,-,sizeof(stepA));
memset(stepB,-,sizeof(stepB));
char tmp[];
for(int i = ; i <= x; i++)
{
scanf("%s",tmp);
for(int j = ; j <= y; j++)
{
if(tmp[j-]=='Y')
{
yx = i;
yy = j;
}
if(tmp[j-]=='M')
{
mx = i;
my = j;
}
g[i][j] = tmp[j-];
}
}
ans = INF; node s, t; s.x = yx;
s.y = yy;
s.step = ;
q.push(s);
while(q.size())
{
s = q.front();
q.pop();
stepA[s.x][s.y] = s.step;
for(int i = ; i < ; i++)
{
t.x = s.x + d[i][];
t.y = s.y + d[i][];
t.step = s.step + ;
if(stepA[t.x][t.y] != -) continue;
if(g[t.x][t.y]=='.'|| g[t.x][t.y]=='@')
q.push(t);
}
}
s.x = mx;
s.y = my;
s.step = ;
q.push(s);
while(q.size())
{
s = q.front();
q.pop();
stepB[s.x][s.y] = s.step;
for(int i = ; i < ; i++)
{
t.x = s.x + d[i][];
t.y = s.y + d[i][];
t.step = s.step + ;
if(stepB[t.x][t.y] != -) continue;
if(g[t.x][t.y]=='.'|| g[t.x][t.y]=='@')
q.push(t);
}
}
for(int i = ; i <= x; i++)
{
for(int j = ; j <= y; j++)
{
if(g[i][j]=='@' && stepA[i][j]+stepB[i][j]<ans && stepA[i][j]!=- && stepB[i][j]!=-)
ans = stepA[i][j]+stepB[i][j];
}
}
cout<<ans*<<endl;
} return ;
}

[kuangbin带你飞]专题一 简单搜索 - N - Find a way的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  2. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  3. [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

    //Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...

  4. [kuangbin带你飞]专题一 简单搜索 棋盘问题

    题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...

  5. [kuangbin带你飞]专题一 简单搜索

            ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题   328 / 854 Problem B POJ 2251 Dungeon Ma ...

  6. [kuangbin带你飞]专题一 简单搜索 回顾总结

    第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队

  7. 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  8. [kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  9. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  10. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

随机推荐

  1. 解决$.ajax请求在ie8下失效问题

    ie8下默认把跨域请求拦截了,需要用jquery.xdomainrequest.min.js 处理跨域问题,需放在jq下引入 http://cdnjs.cloudflare.com/ajax/libs ...

  2. 安装nodejs nvm

    安装nodejs sudo apt-get install nodejs sudo apt-get install npm 安装nvm https://www.runoob.com/w3cnote/n ...

  3. HTML-参考手册: Px、Em 换算工具

    ylbtech-HTML-参考手册: Px.Em 换算工具 1.返回顶部 1. Px.Em 换算工具 以下工具提供了em和px的换算工具. 第一个输入框:设置了网页默认的字体像素 (通常 16px) ...

  4. git分布式版本控制系统权威指南学习笔记(二):git add暂存区的三个状态以及暂存区的理解

    文章目录 不经过git add(到暂存区),能直接进行commit吗? 举个

  5. 剑指offer第二版面试题2:数组中重复的数字(JAVA版)

    题目:在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但是不能修改输入的数组.例如,如果输入长度为8的数组{2,3,5,4,3 ...

  6. 静态成员 static 能被继承吗

    在类定义中,它的成员(包括数据成员和 成员函数)可以用关键字static声明为静 态的,这些成员称为静态成员 静态成员的特性: • 不管这个类创建了多少个对象,静态成员只有一个拷贝,这个拷贝被所有属于 ...

  7. python3 使用int函数将浮点数转换成整数

    int函数将浮点数转换成整数需要注意的地方 >>> int(153)153>>> int(153.4)153>>> int(153.5)153&g ...

  8. 7年Java后端被淘汰,一路北漂辛酸史。。。

    作者:春天花会开foryou oschina.net/question/3465562_2281392 今天分享一位同行的经历: 本人Java开发6年半不到7年的样子. 英语专业,临毕业跟着隔壁专业去 ...

  9. DDOS到底是什么,怎么预防,看看就明白了

    可怕的DDOS怎么预防 分布式拒绝服务(DDoS: distributed denial-of-service)攻击是恶意破坏目标服务器.服务或网络的正常通信量的企图,其方法是用大量Internet通 ...

  10. redis数据结构之SDS

    简介 redis源码虽然是C语言实现的,但是Redis没有直接采用C语言传统的字符串表示,而是构建了一种名叫简单动态字符串(simple dynamic string,SDS)的抽象类型,并将SDS用 ...