正确代码:

 #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. spark面试问题收集

    spark面试问题 1.spark中的RDD是什么,有哪些特性 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可 ...

  2. The Preliminary Contest for ICPC Asia Nanjing 2019( B H F)

    B. super_log 题意:研究一下就是求幂塔函数 %m的值. 思路:扩展欧拉降幂. AC代码: #include<bits/stdc++.h> using namespace std ...

  3. Codeforces 1189A Keanu Reeves

    题目链接:http://codeforces.com/problemset/problem/1189/A 思路:统计1 和 0 的个数,不相等拆开字符串,否则不拆. AC代码: #include< ...

  4. Gerrit(1): Manage Projects

    1) Register an openid account https://login.ubuntu.com/+login 2) Custom settings set SSH pubkey set ...

  5. 2019秋第一次Java学习总结

    本周Java学习总结: 知识点总结: 1.Java中程序的执行步骤 使用Javac将一个.Java源文件编译成.class文件 使用Java可以执行一个*.class文件 2.&&与& ...

  6. 20140730 word标题样式 数组

    1.word 标题四, 右键更新 自己也可以新建标题样式 2.数组 连续内存,空间复杂度高(即使数组存在一个元素,占据的空间的大小不变),时间复杂度低(0(1)访问),内存分配一次性完成

  7. HTML5篇

    [HTML5十大新特性] (1) 语义化标签 (2) 增强型表单 (3) 视频和音频 (4) canvas绘图 (5) SVG绘图 (6) 地理定位 (7) 拖放API (8) Web Worker ...

  8. Dubbo面试20问!这些题你都遇到过吗?

    作者:Dean Wang https://deanwang1943.github.io/bugs/2018/10/05/面试/饿了么/dubbo 面试题/ 1.dubbo是什么 dubbo是一个分布式 ...

  9. python列表中enumerate和zip函数用法

    enumerate: 定义:enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标 例子: list1 =[89,98,00,75,6 ...

  10. C static extern和全局变量

    #include <stdio.h> //默认全局变量为外部变量 int a; //当全局变量前面加上static时,该变量为内部变量 static int b; void test(); ...