/*
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. jqGrid笔记@简单实现

    jqGrid在MVC中的版本是已经通过 HtmlHelper 的扩展方法封装后的产物,webForm版本貌似也将其封装成了服务器端空间,所以我推荐下载原生的jqGrid版本的地址为:http://ww ...

  2. Action的返回值类型总结

    Action的返回值 MVC 中的 ActionResult是其他所有Action返回类型的基类,下面是我总结的返回类型,以相应的帮助方法: 下面是这些方法使用的更详细的例子 一.返回View     ...

  3. QString与中文,QString与std::wstring的相互转换(使用fromStdWString和u8关键字)

    Qt版本:5.5.1 Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 ? 1 QString str("死亡使者赛维"); 这样直接用带中文 ...

  4. use EXPORT和use EXPORT_OK

    我不明白 use EXPORT和use EXPORT_OK的区别,大多数资料提到了一些: @Export 允许 导出模块的函数和变量到用户的名字空间使用标准的导入方法. 这种方式,我们不需要创建模块的 ...

  5. 基于visual Studio2013解决面试题之1204大数组查找

     题目

  6. Android Sqlite数据库执行插入查询更新删除的操作对比

    下面是在Android4.0上,利用Sqlite数据库的insert,query,update,delete函数以及execSql,rawQuery函数执行插入,查询,更新,删除操作花费时间的对比结果 ...

  7. HTTP协议--简析

    HTTP--超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议,是所有的www文件都必须遵守的标准. 要想成为优秀的web开发人员,必须熟悉H ...

  8. webstorm与phpstorm主题配置

    原创. 更换webstorm的主题的,照着网上的教程试了好多次都发现不行,而且我之前有个同学也是这样的问题,找不到相关的colors文件夹,所以在网上教程的基础上对于更改主题做了细微的修改. 1.下载 ...

  9. 基于visual Studio2013解决C语言竞赛题之1067间隔排序

        题目 解决代码及点评 /* 功能:间隔元素排序.用随机函数产生25个[25,75]之间的整数, 把它送到一维数组M中. 要求对M[I],M[I+J],M[I+2*J],-这些元 ...

  10. Godiva_百度百科

    Godiva_百度百科 北京 三里屯 北京市朝阳区三里屯路19号院10号楼一层S10-13单元及二层S10-22单元 100027 北京朝阳大悦城北京市朝阳区朝阳北路101号朝阳大悦城1号商业楼1F- ...