HDU1010(bfs)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int n,m,t,ax,bx;
bool flag;
char plot[9][9];
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void dfs(int a,int b,int tx){
if(a<=0||a>n||b<=0||b>m){
return;}
if(a==ax&&b==b&&tx==t){
flag=1;
return;}
int temp=(t-tx)-abs(a-ax)-abs(b-bx);
if(temp<0||temp&1){
return;}
for(int i=0;i<4;i++){
if(plot[a+dir[i][0]][b+dir[i][1]]!='X'){
plot[a+dir[i][0]][b+dir[i][1]]='X';
dfs(a+dir[i][0],b+dir[i][1],tx+1);
if(flag)
{
return;
}
plot[a+dir[i][0]][b+dir[i][1]] ='.';
}
}
return;
}
int main(){
while(scanf("%d%d%d",&n,&m,&t),n,m,t){
getchar();
int si,sj;
int ss=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&plot[i][j]);
if(plot[i][j]=='S'){si=i;sj=j;}
if(plot[i][j]=='D'){ax=i;bx=j;}
if(plot[i][j]=='X')ss++;
}
getchar();
}
if(n*m-ss<=t){
printf("NO\n");
continue;
}
flag=0;
plot[si][sj]='X';
dfs(si,sj,0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
这里需要剪枝,我参考了一下大牛的解说;
http://acm.hdu.edu.cn/forum/read.php?tid=6158
下面是用javaA过了的
import java.util.Scanner;
public class HDU1010{
static int n,m,t,ax,bx,si,sj;
static char[][]plot;
static boolean flag;
static int dir[][]={{1,0},{0,1},{0,-1},{-1,0}};
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
while(cin.hasNext()){
n=cin.nextInt();
m=cin.nextInt();
t=cin.nextInt();
if(n==0&&m==0&&t==0)
break;
String str[]=new String [n+1];
for(int i=1;i<=n;i++){
str[i]=cin.next();
}
int ss=0;
plot=new char[9][9];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
plot[i][j]=str[i].charAt(j-1);
if(plot[i][j]=='S'){
si=i;sj=j;
}
if(plot[i][j]=='X'){
ss++;
}
if(plot[i][j]=='D'){
ax=i;
bx=j;
}
}
}
if(n*m-ss<=t){
System.out.println("NO");
continue;
}
flag=false;
plot[si][sj]='X';
dfs(si,sj,0);
if(flag){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
return;
}
private static void dfs(int si, int sj, int i) {
if(si<=0||si>n||sj<=0||sj>m){
return;
}
if(si==ax&&sj==bx&&i==t){
flag=true;
return;
}
int temp=(t-i)-Math.abs(si-ax)-Math.abs(sj-bx);
if(temp<0||temp%2==1){
return;
}
for(int j=0;j<4;j++){
if(plot[si+dir[j][0]][sj+dir[j][1]]!='X'){
plot[si+dir[j][0]][sj+dir[j][1]]='X';
dfs(si+dir[j][0],sj+dir[j][1],i+1);
if(flag)
return;
plot[si+dir[j][0]][sj+dir[j][1]]='.';
}
}
return;
}
}
HDU1010(bfs)的更多相关文章
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- WCF - Overview
WCF stands for Windows Communication Foundation. The elementary feature of WCF is interoperability. ...
- Understanding Item Import and Debugging Problems with Item Import (Doc ID 268968.1)
In this Document Purpose Details Scenario 1: Testing the basic item import with minimum columns po ...
- poj棋盘分割(记忆化)
http://poj.org/problem?id=1191 黑书上P116 想了挺久 没想出来 想推出一公式来着 退不出来.. 想偏了 正解:递归 #include <iostream> ...
- nginx错误汇总
一.Nginx出现413 Request Entity Too Large错误解决方法 Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现, ...
- windows下rundll32介绍
最近看书介绍rundll32可以加载dll文件并执行其中导出函数,在MSDN中我们可以看到绍http://support.microsoft.com/kb/164787/zh-cn rundll32调 ...
- EMC
1.EMC的概念 EMC(Electro Magnetic Compatibility)即电磁兼容,是指设备或系统在其电磁环境中符合要求运行并不对其环境中的任何设备产生无法忍受的电磁干扰的能力.就是它 ...
- oracle to_date函数(转载)
TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 ...
- codeforce 621A Wet Shark and Odd and Even
水 最大偶数和 #include<iostream> #include<string> #include<algorithm> #include<cstdli ...
- 数据中心基础设施自动化运维之puppet专项
http://forge.puppetlabs.com/treydock/yum_cron [puppet功能扩展forge] http://docs.puppetlabs.com/referenc ...
- 问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?
问题现象:delphi2007.2010无法二次启动,报EditorLineEnds.ttr被占用,而且进程中一直有bds.exe的进程? 问题原因:问题处理:方法一:可能是系统更新的东东造在的.KB ...