杭州现场赛的题。BFS+DFS

#include <iostream>
#include<cstdio>
#include<cstring>
#define inf 9999999
using namespace std; char mp[105][105];
int sq[5][5];
int step[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct pos
{
int x,y;
}; int n,m,prn,x,y,tmp,ans,cnt;
bool viss[5];
int bfs(pos bg,pos ed)
{
int i;
pos que[20000];
int front=0,rear=1;
bool vis[105][105]={false};
vis[bg.x][bg.y]=true;
que[0]=bg;
int cnt[20000]={0};
if(ed.x==bg.x&&ed.y==bg.y)
return 0;
while(rear>front)
{
for(i=0;i<4;i++)
{
if(que[front].x+step[i][0]>=0&&que[front].x+step[i][0]<n&&que[front].y+step[i][1]>=0&&que[front].y+step[i][1]<m&&
mp[que[front].x+step[i][0]][que[front].y+step[i][1]]!='#'&&!vis[que[front].x+step[i][0]][que[front].y+step[i][1]])
{
que[rear].x=que[front].x+step[i][0];
que[rear].y=que[front].y+step[i][1];
vis[que[rear].x][que[rear].y]=true;
cnt[rear]=cnt[front]+1;
if(que[rear].x==ed.x&&que[rear].y==ed.y)
return cnt[rear];
// cout<<que[rear].x<<' '<<que[rear].y<<endl;
rear++;
}
}
front++;
}
return -1;
}
void cal(int q)
{
int i;
if(cnt==prn)
return;
viss[q]=true;
for(i=1;i<=prn;i++)
{
if(!viss[i])
{
tmp+=sq[q][i];
viss[i]=true;
cnt++;
cal(i);
if(cnt==prn)
if(tmp<ans)
ans=tmp;
cnt--;
tmp-=sq[q][i];
viss[i]=false;
}
}
viss[q]=false;
return;
}
int main()
{
// freopen("in.txt","r",stdin);
while(cin>>n>>m&&(n+m))
{
memset(sq,0,sizeof(sq));
memset(viss,false,sizeof(viss));
int i,j;
ans=inf;
tmp=0;
cnt=0;
pos st;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='@')
{
st.x=i;
st.y=j;
}
}
}
cin>>prn;
pos pr[4];
for(i=0;i<prn;i++)
{
cin>>pr[i].x>>pr[i].y;
pr[i].x--;
pr[i].y--;
// cout<<pr[i].x<<" "<<pr[i].y<<endl;
}
bool f=true;
for(i=0;i<prn;i++)
{
sq[0][i+1]=bfs(st,pr[i]);
if(sq[0][i+1]==-1)
{
f=false;
break;
}
}
if(!f)
{
cout<<-1<<endl;
continue;
}
for(i=0;i<prn;i++)
{
for(j=0;j<prn;j++)
{
if(i==j)
continue;
int t=bfs(pr[i],pr[j]);
sq[i+1][j+1]=sq[j+1][i+1]=t;
}
}
cal(0);
cout<<ans<<endl;
}
return 0;
}

  

2013杭州现场赛B题-Rabbit Kingdom的更多相关文章

  1. HDU 4739 Zhuge Liang's Mines (2013杭州网络赛1002题)

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  4. HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 4816 Bathysphere (2013长春现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 2013长春区域赛的D题. 很简单的几何题,就是给了一条折线. 然后一个矩形窗去截取一部分,求最 ...

  6. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...

  7. 2013 ACMICPC 杭州现场赛 I题

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  8. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

随机推荐

  1. 关于64位WIN7下正确建立JAVA开发环境(转

    1.下载并安装JDK(地址:http://www.oracle.com/technetwor ... ownload-400750.html     先在“Accept License Agreeme ...

  2. jdbc 配置properties实现

    package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...

  3. C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内

    在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候 strWhere += " a.EC101_WRYBH  IN (" + string ...

  4. C#运算除法和求整

    在C#与法中,“/”除后所得的值的类型,跟他的除数和被除数的类型有关.如:                        int a=4;                        int b=5 ...

  5. 设置浏览器使用的dom模式为IE7

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>设置浏览器使用的dom模式为 ...

  6. windows下捕获dump

         一般要捕获异常只需要两个函数:SetUnhandledExceptionFilter截获异常:MiniDumpWriteDump写dump文件.但是由于CRT函数可能会在内部调用SetUnh ...

  7. sp转dp dp转px

    (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, context.getResources().getDis ...

  8. 修改Widows网络设置提升网速

    可能很多用户不知道,我们在使用Windows系统连接Internet,系统默认保留20%的带宽,也就是说我们进行网络数据传输所能使用的带框仅为实际带宽的80%,但是我们修改网络设置或的最大带宽. 1. ...

  9. 牛场围栏(vijos 1054)

    题目大意: 给出N种木棍(每种木棍数量无限)的长度(<=3000),每根木棍可以把它切掉[1,M]的长度来得到新的木棍. 求最大的不能被组合出来的长度. 如果任何长度都能组合出来或者最大值没有上 ...

  10. 关于java.lang.reflect.InvocationTargetException

    今天遇到java.lang.reflect.InvocationTargetException错误,卡了好一会儿,报错代码 try { Class<?> c= Class.forName( ...