正确代码:

 #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. 关于performSelector调用和直接调用区别

    下面两段代码都在主线程中运行,我们在看别人代码时会发现有时会直接调用,有时会利用performSelector调用,今天看到有人在问这个问题,我便做一下总结, [delegate imageDownl ...

  2. JS互相调用

    JS互相调用 例1: <html> <head> <meta charset="UTF-8"> <script type="te ...

  3. TFS 中如何将项目加入已有的源代码管理器中?

    Visual Studio 的某解决方案已经加入 Team Foundation Server,现在再将已经存在的项目加入到解决方案中,可是签入时,并没有把新加入的项目签入,怎么办呢? 在团队资源管理 ...

  4. could not stop cortex-m device

    检查一下STM32复位管脚是不是0V,如果是0V的话并且你有上拉电阻,那么就断电后检查一下STM32的VCC和GND是否短路,我的就是两个贴片电容击穿造成的短路从而使RST无法拉高.

  5. Codeforces 1189C Candies!

    题目链接:http://codeforces.com/problemset/problem/1189/C 思路:前缀和. AC代码: #include<bits/stdc++.h> usi ...

  6. java-day25

    . 标签学习:         1. 文件标签:构成html最基本的标签             * html:html文档的根标签             * head:头标签.用于指定html文档 ...

  7. 30-Ubuntu-用户权限-01-用户和权限的基本概念

    1.用户 用户是Linux系统工作中重要的一环,用户管理包括用户和组管理. 在Linux系统中,不论是由本机或是远程管理登录系统,每个系统都必须拥有一个账号,并且对于不同的系统资源拥有不同的使用权限. ...

  8. HXY烧情侣

    题目描述 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里有n座电影院,n对情侣分别在每座电影院里,然后电影院里都有汽油,但是要 ...

  9. python--面向对象:多态与封装

    一.多态 :python 天生支持多态多态指的是一类事物有多种形态 eg:文件有多种形态:文本文件,可执行文件鸭子类型:python中崇尚鸭子类型,不崇尚根据继承所得来的相似 优点 : 松耦合 每个相 ...

  10. Pregel Worker