HDU1035深度搜索
/*
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深度搜索的更多相关文章
- F - 蜘蛛牌(深度搜索)
Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
- #C++初学记录(深度搜索#递归)
深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...
- 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)
#include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...
- [LeetCode] Populating Next Right Pointers in Each Node 深度搜索
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- ListView 使用方法(Asp.Net)
您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online 文件夹 L ...
- swift 自定义TabBarItem
1.效果图 2.NewsViewController.swift // // NewsViewController.swift // NavigationDemo // // Created ...
- android开发隐藏了actionbar仍然短暂闪现的解决方法
有时候我们在代码里隐藏了actionbar,在打开应用时,仍然短暂闪现下actionbar,用户体验很不好. 最简单的方法是 在AndroidManifest.xml中设置主题中配置不显示titl ...
- linux: /usr/bin/ld: cannot find -lloc
/usr/bin/ld: cannot find -lloc ld链接库的时候没发现loc这个库-lloc本事不是文件名字,要去找这个库就搜索libloc, loc, 不能搜索lloc. /usr1/ ...
- 使用Java创建RESTful Web Service(转)
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- static在C和C++中的用法和区别
static主要有三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数据成员/成员函数 前两种C和C++都有,第三种仅在C++中有,下面分别作以下介绍: 一.局部静态变量 在C/C+ ...
- 基于visual Studio2013解决面试题之1405归并排序
题目
- 如何把 excel 的数据导入到数据库里面去
1. 把 excel 另存为 .csv 格式 2. 用 Notepad 打开 .csv 文件, 第一行就是全部的字段 3. 创建表结构 create table yu_rt_01 as select ...
- [转]java-Three Rules for Effective Exception Handling
主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...
- Spring3.0 入门进阶(1):从配置文件装载Bean
Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅. 作为入门,本篇主要介绍Bean的加载 ...