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秒.关于康托展开可以 ...
随机推荐
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...
- java1.8的几大新特性(一)
一.接口的默认方法与静态方法,也就是接口中可以有实现方法 public class Test { public static void main(String[] args) { Formula a= ...
- 手势识别官方教程(6)识别拖拽手势用GestureDetector.SimpleOnGestureListener和onTouchEvent
三种现实drag方式 1,在3.0以后可以直接用 View.OnDragListener (在onTouchEvent中调用某个view的startDrag()) 2,onTouchEvent() ...
- com.google.common.eventbus.EventBus介绍
以下内容直接翻译了EventBus的注释: com.google.common.eventbus.EventBus介绍: 首先这个类是线程安全的, 分发事件到监听器,并提供相应的方式让监听器注册它们自 ...
- Memcached‘process_bin_delete’函数安全漏洞
漏洞名称: Memcached‘process_bin_delete’函数安全漏洞 CNNVD编号: CNNVD-201401-174 发布时间: 2014-01-15 更新时间: 2014-01-1 ...
- CDN原理
要了解CDN的实现原理,首先让我们来回顾一下网站传统的访问过程,以便理解其与CDN访问方式之间的差别: 由上图可见,传统的网站访问过程为:1. 用户在浏览器中输入要访问的域名:2. 浏览器向域名解析服 ...
- ASP.NET操作Word的IIS权限配置
ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...
- c#抓取当前电脑显示分辨率
using System.Windows.Forms; 获取屏幕分辨率 int SH = Screen.PrimaryScreen.Bounds.Height; ...
- oracle中查看某个用户名下所有的表以及sequence
select table_name from all_tables where owner =upper('jdfp') ; 此处查的是tieba这个用户表空间下的所有表 ...
- 【HTML】Advanced5:Accessible Forms
1.label <form> <label for="yourName">Your Name</label> <input name=&q ...