## hdu2612Find a way(dbfs或者IDA*)

2019年01月13日 ⁄ 综合 ⁄ 共 4235字 ⁄ 字号 评论关闭

# Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2648    Accepted Submission(s): 848

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.

Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘@’ KCF

Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.

Sample Input
```4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#```

Sample Output
```66
88
66```

Author
yifenfei

Source

Recommend
yifenfei

dbfs:

```#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N = 205;
const int M = 10005;//205就够了。。。这数据是有多弱。。。Orz。。。
int n,m;
char map[N][N];
int flag[2][N][N];
int si,sj,ei,ej;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
struct node
{
int x,y,time;
}ss,now;
struct que
{
struct node t[M];
void init()
{
}
bool empty()
{
}
void push(struct node a)
{
t[tail] = a;
tail ++;
if(tail >= M)
tail -= M;
}
void pop()
{
}
struct node top()
{
}
}q[2];

int Min(int a,int b)
{
return a > b?b:a;
}

int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m) != EOF)
{
getchar();
for(i = 1;i <= n;i ++)
{
for(j = 1;j <= m;j ++)
{
map[i][j] = getchar();
if(map[i][j] == 'Y')
{
si = i;
sj = j;
}
if(map[i][j] == 'M')
{
ei = i;
ej = j;
}
}
getchar();
}
q[0].init();
q[1].init();
ss.time = 0;
ss.x = si;
ss.y = sj;
memset(flag,0,sizeof(flag));
flag[0][si][sj] = 1;
q[0].push(ss);
ss.x = ei;
ss.y = ej;
flag[1][ei][ej] = 1;
q[1].push(ss);
j = 0;
int ans = 0x3fff;
while(!q[0].empty() && !q[1].empty())
{
for(i = 0;i <= 1;i ++)
{
while(!q[i].empty() && q[i].top().time == j)
{
now = q[i].top();
q[i].pop();
ss.time = now.time + 1;
for(k = 0;k < 4;k ++)
{
ss.x = now.x + dx[k];
ss.y = now.y + dy[k];
if(ss.x < 1 || ss.y < 1 || ss.x > n || ss.y > m || map[ss.x][ss.y] == '#')
continue;
if(!flag[i][ss.x][ss.y])
{
flag[i][ss.x][ss.y] = ss.time;
if(flag[1 - i][ss.x][ss.y] && map[ss.x][ss.y] == '@')
{
ans = Min(ans,flag[1 - i][ss.x][ss.y] + ss.time);
break;
}
q[i].push(ss);
}
}
}
}
j ++;
}
printf("%d\n",ans * 11);
}
return 0;
}
//15MS	1252K
/*
5 6
Y.#...
@.....
...@#.
...##.
.....M
*/```

IDA*:

```#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N = 205;
const int M = 1000005;
int n,m;
int ans;
char map[N][N];
int flag[2][N][N];//这题判不判重都行
int si,sj,ei,ej;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
bool ok;

int Abs(int a)
{
return a > 0?a:-a;
}

void Dfs(int curi,int curj,int dp)
{
if(ok)
return;
if(dp + Abs(curi - ei) + Abs(curj - ej) > ans)
return;
if(map[curi][curj] == 'M')
{
ok = true;
return;
}
int i;
//flag[1][curi][curj] = 1;
for(i = 0;i < 4;i ++)
{
int tx = curi + dx[i];
int ty = curj + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || map[tx][ty] == '#')// || flag[1][tx][ty])
continue;
Dfs(tx,ty,dp + 1);
}
//flag[1][curi][curj] = 0;
}

void dfs(int curi,int curj,int dp)
{
if(ok)
return;
if(dp + Abs(curi - ei) + Abs(curj - ej) > ans)
return;
//flag[0][curi][curj] = 1;
if(map[curi][curj] == '@')
{
Dfs(curi,curj,dp);
}
int i;
for(i = 0;i < 4;i ++)
{
int tx = curi + dx[i];
int ty = curj + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || map[tx][ty] == '#')// || flag[0][tx][ty])
continue;
dfs(tx,ty,dp + 1);
}
//flag[0][curi][curj] = 0;
}

int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m) != EOF)
{
//memset(flag,0,sizeof(flag));
getchar();
for(i = 1;i <= n;i ++)
{
for(j = 1;j <= m;j ++)
{
map[i][j] = getchar();
if(map[i][j] == 'Y')
{
si = i;
sj = j;
}
if(map[i][j] == 'M')
{
ei = i;
ej = j;
}
//                if(map[i][j] == '#')
//                    flag[0][i][j] = flag[1][i][j] = 1;
}
getchar();
}
ans = Abs(si - ei) + Abs(sj - ej);//迭代初始深度不必从1开始
ok = false;
while(!ok)
{
dfs(si,sj,0);
ans ++;
}
printf("%d\n",ans * 11 - 11);
}
return 0;
}
//265MS	440K
//203MS	604K
//为什么会跑这么慢。。。
```