poj1111 DFS

J - 搜索

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 1111

Description

Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected for analysis by a mouse click on the object. The perimeter of the boundary of an object is one useful measure. Your task is to determine this perimeter for selected objects.

The digitized slides will be represented by a rectangular grid of periods, ‘.‘, indicating empty space, and the capital letter ‘X‘, indicating part of an object. Simple examples are

XX   Grid 1       .XXX   Grid 2 XX                .XXX                   .XXX                   ...X                   ..X.
 X... 

An X in a grid square indicates that the entire grid square, including its boundaries, lies in some object. The X in the center of the grid below is adjacent to the X in any of the 8 positions around it. The grid squares for any two adjacent X‘s overlap on an edge or corner, so they are connected.

XXX XXX    Central X and adjacent X‘s XXX 

An object consists of the grid squares of all X‘s that can be linked to one another through a sequence of adjacent X‘s. In Grid 1, the whole grid is filled by one object. In Grid 2 there are two objects. One object contains only the lower left grid square. The remaining X‘s belong to the other object.

The technician will always click on an X, selecting the object containing that X. The coordinates of the click are recorded. Rows and columns are numbered starting from 1 in the upper left hand corner. The technician could select the object in Grid 1 by clicking on row 2 and column 2. The larger object in Grid 2 could be selected by clicking on row 2, column 3. The click could not be on row 4, column 3.

One useful statistic is the perimeter of the object. Assume each X corresponds to a square one unit on each side. Hence the object in Grid 1 has perimeter 8 (2 on each of four sides). The perimeter for the larger object in Grid 2 is illustrated in the figure at the left. The length is 18.

Objects will not contain any totally enclosed holes, so the leftmost grid patterns shown below could NOT appear. The variations on the right could appear:

Impossible   Possible 

XXXX         XXXX   XXXX   XXXX X..X         XXXX   X...   X...
XX.X         XXXX   XX.X   XX.X XXXX         XXXX   XXXX   XX.X 

.....        .....  .....  .....
..X..        ..X..  ..X..  ..X..
.X.X.        .XXX.  .X...  .....
..X..        ..X..  ..X..  ..X..
.....        .....  .....  ..... 

Input

The input will contain one or more grids. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the mouse click. All numbers are in the range 1-20. The rows of the grid follow, starting on the next line, consisting of ‘.‘ and ‘X‘ characters.

The end of the input is indicated by a line containing four zeros. The numbers on any one line are separated by blanks. The grid rows contain no blanks.

Output

For each grid in the input, the output contains a single line with the perimeter of the specified object.

Sample Input

2 2 2 2
XX
XX
6 4 2 3
.XXX
.XXX
.XXX
...X
..X.
X...
5 6 1 3
.XXXX.
X....X
..XX.X
.X...X
..XXX.
7 7 2 6
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
7 7 4 4
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
0 0 0 0

Sample Output

8
18
40
48
8题目大意:给你一个矩形图,上面有着用X组成的物体,每一个X与他周围8个方向上的X算是相邻,所有相邻的X算作一个物体,题目会给你搜索的起点,以此确定到底是哪一个物体,然后让你输出该物体的周长,每一个X的边长为1,结合样例很容易明白其周长的计算方法。思路分析:首先是,周长应该通过什么来进行确定,一个X上下左右不是‘X‘就‘.‘,X说明相邻,‘.‘则说明那一侧是边界,所以说可以通过计算该物体中的X周‘.‘的个数来间接求出其周长。注意应该开两个方向数组,一个是8个方向,用来寻找下一个X,进行深搜,一个是4个方向,用来寻找边界,计算周长。代码:#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <stack>using namespace std;const int maxn=105;char maps[maxn][maxn];int f[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};int m,n,p,q;int cnt,flag;void dfs(int x,int y){   maps[x][y]=‘*‘;   for(int i=0;i<4;i++)   {       int a=x+f[i][0];       int b=y+f[i][1];       if(maps[a][b]==‘.‘) cnt++;   }   for(int i=0;i<8;i++)   {       int a=x+f[i][0];       int b=y+f[i][1];       if(maps[a][b]==‘X‘) dfs(a,b);   }

}int main(){    while(scanf("%d%d%d%d",&m,&n,&p,&q)&&(m||n||p||q))    {
        cnt=0;        int i,j;        memset(maps,‘.‘,sizeof(maps));        for(i=1;i<=m;i++)
            for(j=1;j<=n;j++)            cin>>maps[i][j];        dfs(p,q);        cout<<cnt<<endl;    }}深搜就是状态的转移,你所要做的就是确定两个,一个是在这个状态你要做什么,另一就是如何判断是否进入下一个状态。永不停息!
时间: 2024-10-28 11:33:42

poj1111 DFS的相关文章

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

uva1103(dfs)

UVA - 1103 还是没写好,,看的别人的 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <str

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

蓝桥杯 大臣的旅费_树的最长度_两次DFS

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <functional> #include <vector> using namespace std; const int maxn = 1000000 + 10; const int INF = 10000000

A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出. 这样写起来比较好写. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <

codeforces717E Paint it really, really dark gray(树上dfs)

题意: 给你一棵树,2e5个节点,每个节点有一种颜色(黑色或粉色) 让你从节点1开始,自由沿边行走,到达节点时会把这个节点的颜色改变 要求你输出任意一条路径使得从节点1出发,所有节点的颜色都变为黑色 思路: 很明显要递归遍历 每到达一个节点就先改变节点的颜色标志并输出当前节点 如果当前到达了叶子节点,则不用进行操作 返回上层节点时再改变节点的颜色标志并输出当前节点,然后判断叶子节点的颜色,如果为粉色,则再到达一次叶子节点再回到当前节点 这样确保当前节点的所有儿子节点都为黑色的 然后就这样一直递归

POJ2488 dfs

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41972   Accepted: 14286 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

BZOJ_1016_[JSOI2008]_最小生成树计数_(dfs+乘法原理)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1016 给出一张图,其中具有相同权值的边的数目不超过10,求最小生成树的个数. 分析 生成树的计数有一个什么什么算法... 我真的企图研究了...但是智商捉急的我实在看不懂论文... 所以最后还是写了暴力... 当然暴力也要靠正确的姿势的. 首先来看一个结论: 同一张图的所有最小生成树中,边权值相同的边的数目是一定的. 也就是说,假如某一张图的某一棵最小生成树由边权值为1,1,2,2,2,3的