首页 > 技术文章 > uva 657

hanbinggan 2015-01-17 11:29 原文

很简单的题,就是题意不懂……!

就是判断每个'*'区域内‘X’区域块的个数

WA了好多次,就是太差了;

1.结果排序输出

2.因为是骰子所以不再1-6范围内的数字要舍弃

3.格式要求要空一行……

4.因为碰到X就清零了,所以

XXX*X
XXX*X
.....
X***X
XX***

把X清零之后,在原来的*区域内的dfs就进行不下去了,就wa了……233333333

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map>

using namespace std;
const int INF = 0xffffff;
const double Pi = 4 * atan(1);
const int Maxn = 200 + 10;
//int dir2[8][2] = {{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{1,1}};
int dr[] = {0,1,0,-1};
int dc[] = {1,0,-1,0};
int h,w;
char graph[60][60];
int a[2500];
int cnt;
int num;

void dfs2(int r,int c){
    graph[r][c] = '#';
    for(int i = 0;i < 4;i++){
        int xx = r + dr[i];
        int yy = c + dc[i];
        if(xx > -1 && yy > -1 && xx < h && yy < w){
            if(graph[xx][yy] == 'X'){
                dfs2(xx,yy);
            }
        }
    }
}

void dfs(int r,int c){
    if(graph[r][c] == 'X'){
        dfs2(r,c);
        num++;
    }
    graph[r][c] = ' ';
    for(int i = 0;i < 4;i++){
        int xx = r + dr[i];
        int yy = c + dc[i];
        if(xx > -1 && yy > -1 && xx < h && yy < w){
            if(graph[xx][yy] == '*' || graph[xx][yy] == 'X' || graph[xx][yy] == '#')
                dfs(xx,yy);
        }
    }
}

int cmp(const void * a,const void * b){
    return *((int *)a) - *((int *)b);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("inpt.txt","r",stdin);
#endif
    int cas = 0;
    while(cin >> w >> h){
        if(!w && !h)
            break;
        memset(a,0,sizeof(a));
        cnt = 0;
        for(int i = 0;i < h;i++)
            cin >> graph[i];
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                if(graph[i][j] == '*' || graph[i][j] == 'X'){
                    num = 0;
                    dfs(i,j);
                    if(num > 0 && num < 7)
                        a[cnt++] = num;
                }
            }
        }
        qsort(a,cnt,sizeof(int),cmp);
        cout << "Throw " << ++cas << endl;
        for(int i = 0;i < cnt-1;i++)
            cout << a[i] << " ";
        cout << a[cnt-1] << endl << endl;
    }
    return 0;
}
View Code

测试用例:

5 5
*.***
***..
.....
.....
.....
5 5
XXX*X
XXX*X
.....
X***X
XX***
10 5
..........
..X**.*X..
..........
...*X*X...
..........
10 5
..........
..X....X..
..........
...*X*X...
..........
10 5
..........
..X....X..
..X....X..
..XXXXXX..
..........
10 5
..........
..X*X.....
..*X*.....
..X*X.....
..........
10 5
..........
..X*X.....
..*X**....
..X*X*....
..........
5 5
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
30 15
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
10 6
..........
.XXX......
....XXX...
.......XXX
....XXX...
*X*X......
6 6
XXXXX*
.....X
.....X
.....X
.....X
.....X
6 6
XXXXX.
.....X
.....X
.....X
.....X
.....X
6 6
XXXXX.
....*X
.....X
.....X
.....X
.....X
30 15
.....X*X*X*X*X*X***...........
.X......................X.....
...............*.........X....
...X****......****........X...
...*X*.*.....**X***X..........
...*.X......***X**.....XXX....
...*.*X*.....****........X....
...***.X.......*.........X....
..............................
.......X***.............*..***
......******X****.....*X**X*..
..***********......**.*.*.....
.....****X**.......*X**X*.....
........***........*....*.....
........***.********..........
0 0
View Code

ans:

Throw 1
0

Throw 2
2 2

Throw 3
1 1 2

Throw 4
1 1 2

Throw 5
1

Throw 6
5

Throw 7
5

Throw 8
1

Throw 9
1 2 2 4

Throw 10
1 1 1 1 2

Throw 11
2

Throw 12
1 1

Throw 13
2

Throw 14
1 1 1 1 1 2 3 4 5 6

 

推荐阅读