/*
HDU1035
意甲冠军:
给定一个字符矩阵,N S W E分别代表向上,下,剩下,进
模拟搜索,推断:
若能走出字符矩阵。则Yes,输出步数
若走不出矩阵,那么必然有圈存在,必然在矩阵中存在一个点会訪问第二次
*/ #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
using namespace std; #define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","w",stdout)
#define For1(i,a,b) for (i=a;i<b;i++)
#define For2(i,a,b) for (i=a;i<=b;i++)
#define Dec(i,a,b) for (i=a;i>b;i--)
#define Dec2(i,a,b) for (i=a;i>=b;i--)
#define Sca_d(x) scanf("%d",&x)
#define Sca_s(x) scanf("%s",x)
#define Sca_c(x) scanf("%c",&x)
#define Sca_f(x) scanf("%f",&x)
#define Sca_lf(x) scanf("%lf",&x)
#define Fill(x,a) memset(x,a,sizeof(x))
#define MAXN 1110
#define MAXM 1110
#define MAXINT 111111 template <typename T>
T gcd(T a,T b)
{
return b==0?a:gcd(b,a%b);
} template <typename T>
T lcm(T a,T b)
{
return a/gcd(a,b)*b;
} char dir_ch[5]={' ','W','S','E','N'};
int dir_x[5]={0,0,1,0,-1};
int dir_y[5]={0,-1,0,1,0};
//三个常量数组模拟上下左右的字母行动 char map[MAXN][MAXM];
int dist[MAXN][MAXM];
char ch[MAXM];
int m,n,enter; void dfs(int x,int y)
{
int newx,newy,k;
For2(k,1,4)
if (map[x][y]==dir_ch[k])//等于哪个方向。就往那个方向走
{
newx=x+dir_x[k];
newy=y+dir_y[k];
if (newx<1||newx>n||newy>m||newy<1)//假设跑出了地图之外,则已经出了矩阵
{
printf("%d step(s) to exit\n",dist[x][y]);
return;
}
if (!dist[newx][newy])
{
dist[newx][newy]=dist[x][y]+1;//没到目标继续搜索
dfs(newx,newy);
}
else
{
printf("%d step(s) before a loop of %d step(s)\n",
dist[newx][newy]-1,dist[x][y]+1-dist[newx][newy]);
//假设当前产生的子节点之前已经訪问过
//那么。必然在原图之中产生了环。 //当中,环的长度为 dist[x][y]+1-dist[newx][newy]
//从 dist[newx][newy]-1 步后開始出现环
return;
}
}
return;
} int main()
{
//input;
int i,j,k;
while(cin>>n>>m>>enter)
{
if (!n&&!m) break;
Fill(dist,0);
For2(i,1,n)
{
Sca_s(ch);
For2(j,1,m)
map[i][j]=ch[j-1];
}
dist[1][enter]=1;
dfs(1,enter);
}
return 0;
}
/*
附:在问题的例子Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0
*/

HDU1035深度搜索的更多相关文章

  1. F - 蜘蛛牌(深度搜索)

    Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...

  2. 题目--oil Deposits(油田) 基础DFS(深度搜索)

    上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...

  3. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  4. 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)

    #include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...

  5. [LeetCode] Populating Next Right Pointers in Each Node 深度搜索

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  6. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  8. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

随机推荐

  1. hdu 4861 Couple doubi(数论)

    题目链接:hdu 4861 Couple doubi 题目大意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+⋯+(p−1)i%p,两个人轮流取,假设DouBiNan的值大的话就输出YES, ...

  2. Objective-C基础笔记(5)Protocol

    Protocol简单来说就是一系列方法的列表,当中声明的方法能够被不论什么类实现.这中模式一般称为代理(delegation)模式. 在IOS和OS X开发中,Apple採用了大量的代理模式来实现MV ...

  3. .NET vs JAVA

    一个同事写一个方案,让我补充下.NET和 JAVA语言的优缺点,以下是我的回复: 老X你好! 我觉得这个问题,本质上不是java和.net两个开发语言方面的比较,单纯从这两个开发语言来讲,部分伯仲,在 ...

  4. Linux的五个查找命令 [转]

    最近,我在学习Linux,下面是一些笔记. 使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序 ...

  5. poj3356 AGTC

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  6. Thymeleaf Javascript 取值

    <script th:inline="javascript"> var openid = /*[[${session.wxuser.openId}]]*/ </s ...

  7. 14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制:

    14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制: MySQL 复制工作对于InnoDB 表和对于MyISAM表. 它是可能使用复制的方式 存储引 ...

  8. GDI 总结三: CImage类使用

    前言          CImage类是基于GDI+的.可是这里为什么要讲归于GDI? 主要是基于这种考虑: 在GDI+环境中,我们能够直接使用GDI+ ,没多少必要再使用CImage类 可是,假设再 ...

  9. 【免费讲座IX算法第一阶段】转专业找CS工作“打狗棒法”

    个人经验CS不相干,如何收拾简历?如何获取知识,在最短的时间内找到一份工作需要?如何避免盲目刷称号,迅速制定学习计划?如何准备面试? 星期五.九算法黄蓉老师受邀嘉宾 [在线共享] 她成功转专业的六个月 ...

  10. POJ2782:Bin Packing

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...