HLJUOJ1023(四方向搜索 + 八方向搜索)

来源:互联网 发布:vb中的对象 编辑:程序博客网 时间:2024/05/01 05:17

1023: Balloons

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 11  Solved: 5
[Submit][Status][Web Board]

Description

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N≤100), which represents the size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case is followed by a line containing one zero.

Output

For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

511001001001111111010100100

Sample Output

Case 1: 3 2


解题思路:
题意是说0代表不能走,1代表能走,问1所能构成的联通块。两个人的搜索方式不同,第一个人按四个方向搜索,第二个人按八个方向搜索。
暴力每个点,把联通的都改成0,计数器+1,。因为涉及到图的修改,所以开了一个copy_maps来保存原图。



完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 1111;int maps[maxn][maxn];int copy_maps[maxn][maxn];int n;string str;int vis[maxn][maxn];int d1[4][2] = { -1 , 0 , 0 , 1 , 1 , 0 , 0 , -1 };int d2[8][2] = { -1 , 0 , -1 , 1 , 0 , 1 , 1 ,1 , 1 , 0 , 1 , -1 , 0 , -1 , -1 , -1};void dfs1(int x , int y){    vis[x][y] = 1;    for(int i = 0 ; i < 4 ; i ++)    {        int xx = x + d1[i][0];        int yy = y + d1[i][1];        if(xx >= 0 && xx < n && yy >= 0 && yy < n && !vis[xx][yy] && maps[xx][yy] == 1)        {            maps[xx][yy] = 0;            dfs1(xx , yy);        }    }}void dfs2(int x , int y){    vis[x][y] = 1;    for(int i = 0 ; i < 8 ; i ++)    {        int xx = x + d2[i][0];        int yy = y + d2[i][1];        if(xx >= 0 && xx < n && yy >= 0 && yy < n && !vis[xx][yy] && copy_maps[xx][yy] == 1)        {            copy_maps[xx][yy] = 0;            dfs2(xx , yy);        }    }}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int cas = 1;    while(~scanf("%d",&n))    {        if(n == 0)            break;        for(int i = 0 ; i < n ; i ++)        {            cin >> str;            for(int j = 0 ; j < n ; j ++)            {                maps[i][j] = str[j] - '0';                copy_maps[i][j] = maps[i][j];            }        }        int cnt1 = 0 , cnt2 = 0;        memset(vis , 0 , sizeof(vis));        for(int i = 0 ; i < n ; i ++)        {            for(int j = 0 ; j < n ; j ++)            {                if(maps[i][j] == 1)                {                    dfs1(i , j);                    cnt1 ++;                }            }        }        memset(vis , 0 , sizeof(vis));        for(int i = 0 ; i < n ; i ++)        {            for(int j = 0 ; j < n ; j ++)            {                if(copy_maps[i][j] == 1)                {                    dfs2(i , j);                    cnt2 ++;                }            }        }        printf("Case %d: %d %d\n\n" , cas ++ , cnt1 , cnt2);    }}


0 0