工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 915|回复: 1

[搜索题]zju1047

[复制链接]
发表于 2005-7-20 22:12 | 显示全部楼层 |阅读模式
应该是最简单的搜索题目了吧
http://acm.zju.edu.cn/show_problem.php?pid=1047
--------------------------------------------------------------------------------

Image Perimeters

--------------------------------------------------------------------------------

Time limit: 1 Seconds   Memory limit: 32768K   
Total Submit: 1807   Accepted Submit: 962   

--------------------------------------------------------------------------------

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



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:



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.

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


Example 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


Example output:

8
18
40
48
8


--------------------------------------------------------------------------------

Submit   Back   Status

--------------------------------------------------------------------------------

Zhejiang University Online Judge V1.0
 楼主| 发表于 2005-7-21 09:13 | 显示全部楼层
#include <iostream>
#include <queue>
#include <fstream>

using namespace std;

class point
{
        public :
                int m_x;
                int m_y;
        point(int x, int y)
        {
                m_x = x;
                m_y = y;
        };
};
int main()
{
        //ifstream cin(\"in.txt\");
        //ofstream cout(\"out.txt\");
        char map[22][22];
        bool visited[22][22];
       
        int row,col,x,y,perimeter;
        queue<point> myqueue;
        while(1)
        {
                cin >> row >> col >> x >> y;
                if(row == 0 && col == 0)
                {
                        break;
                }
                else
                {
                        perimeter = 0;
                        int i,j;
                        for(i = 1 ;i <= row;i++ )
                        {
                                for(j = 1;j <= col;j++)
                                {
                                        cin >> map[j];
                                       
                                }
                        }
                        for(i = 0 ;i < 22;i++ )
                        {
                                for(j = 0;j < 22;j++)
                                {
                                        visited[j]= false;
                                       
                                }
                        }
                        point temp(x,y);
                        myqueue.push(temp);
                        visited[x][y] = true;
                        while(!myqueue.empty())
                        {
                                //four direction
                                if(x + 1 > row || map[x+1][y] ==\'.\')
                                {
                                        perimeter++;
                                }
                                else
                                {
                                       
                                        if(x+1 <= row && !visited[x+1][y] && map[x+1][y] == \'X\')
                                        {
                                                point temp2(x+1,y);
                                                myqueue.push(temp2);
                                                visited[x+1][y] = true;       
                                        }
                                }
                               
                                if(x - 1 <= 0 || map[x-1][y] == \'.\')
                                {
                                        perimeter++;
                                               
                                }
                                else
                                {
                                        if(x - 1 >=1 && !visited[x-1][y] && map[x-1][y] == \'X\')
                                        {
                                                point temp3(x-1,y);
                                                myqueue.push(temp3);
                                                visited[x-1][y] = true;
                                        }
                                }
                               
                                if(y + 1 > col || map[x][y+1] == \'.\')
                                {
                                        perimeter++;
                                }
                                else
                                {
                                       
                                        if(y+1 <=col && !visited[x][y+1] && map[x][y+1] == \'X\')
                                        {
                                                point temp4(x,y+1);
                                                myqueue.push(temp4);
                                                visited[x][y+1] = true;       
                                        }
                                }
                                if(y - 1 <= 0 || map[x][y-1] == \'.\')
                                {
                                        perimeter++;
                                               
                                }
                                else
                                {
                                        if(y - 1 >=1 && !visited[x][y-1] && map[x][y-1] == \'X\')
                                        {
                                                point temp5(x,y-1);
                                                myqueue.push(temp5);
                                                visited[x][y-1] = true;
                                        }
                                }
                                //fout corners
                                if(x + 1 <= row  && y + 1 <= col)
                                {
                                        if( !visited[x+1][y+1] && map[x+1][y+1] == \'X\')
                                        {
                                                point temp6(x+1,y+1);
                                                myqueue.push(temp6);
                                                visited[x+1][y+1] = true;
                                        }
                                               
                                }
                               
                                if(x + 1 <= row  && y - 1 >= 1)
                                {
                                        if( !visited[x+1][y-1] && map[x+1][y-1] == \'X\')
                                        {
                                                point temp7(x+1,y-1);
                                                myqueue.push(temp7);
                                                visited[x+1][y-1] = true;
                                        }
                                               
                                }
                               
                                if(x - 1 >= 1  && y + 1 <= col)
                                {
                                        if( !visited[x-1][y+1] && map[x-1][y+1] == \'X\')
                                        {
                                                point temp8(x-1,y+1);
                                                myqueue.push(temp8);
                                                visited[x-1][y+1] = true;
                                        }
                                               
                                }
                               
                                if(x - 1 >= 1  && y - 1 >= 1)
                                {
                                        if( !visited[x-1][y-1] && map[x-1][y-1] == \'X\')
                                        {
                                                point temp9(x-1,y-1);
                                                myqueue.push(temp9);
                                                visited[x-1][y-1] = true;
                                        }
                                               
                                }
                               
                                if( !myqueue.empty())
                                {
                                        myqueue.pop();
                                }
                                if( !myqueue.empty())
                                {
                                        x = myqueue.front().m_x;
                                        y = myqueue.front().m_y;
                                }
                        }
                        cout << perimeter<<endl;
                }
        }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-6-5 05:11

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表