HUNNU11398:Reverse Nonogram

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11398&courseid=0

A Nonogram is a pencil puzzle played on a grid. The grid is initially blank. There are
numbers on the side and top of the grid, which indicate how the grid squares should be
filled in. The numbers measure how many unbroken lines of filled-in squares there are
in any given row or column. For example, a clue of "4 8 3" would mean there are sets of
four, eight, and three filled squares, in that order, with at least one blank square
between successive groups. Here is a small example, with its solution.
              
You are going to work backwards.  Given a Nonogram solution, produce the numbers
which should be at the side and top of the grid.
Input
There will be several test cases  in the  input. Each test case will begin with an integer n
(2≤n≤100) indicating the size of the grid. Each of the next  n  lines will have exactly  n
characters, consisting of either '.' for a blank square, or 'X' for a square which has been
filled in. The input will end with a line with a single 0.
Output
For each test case, print 2n  lines of output. The first n  lines represent the numbers for
the rows, from top to bottom. The next n  lines represent the numbers across the top,
from left to right. If any row or column has no squares filled in, output a 0. Put a single
space between numbers on the same line. Do not output any lines with leading or
trailing blanks. Do not output blank lines between any lines of output.

Sample Input
3
XXX
.XX
.X.
3
X.X
..X
X..
5
..X..
.XXX.
X.X.X
..X..
..X..
0
 
Sample Output
3
2
1
1
3
2
1 1
1
1
1 1
0
2
1
3
1 1 1
1
1
1
1
5
1
1

 

题意:题目废话很多,没看懂,看样例看懂了- -,就是先按行统计连续X的个数,再按列统计连续X的个数,每统计一次输出一次

例如X.X.X因为第一个X只有一个连续,第二第三个也是如此,所以是1 1 1

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char map[105][105];

int main()
{
    int n,i,j,ans;
    while(~scanf("%d",&n),n)
    {
        memset(map,'\0',sizeof(map));
        for(i = 0; i<n; i++)
            scanf("%s",map[i]);
        for(i = 0; i<n; i++)
        {
            ans = 0;
            int cnt = 0;
            for(j = 0; j<n; j++)
            {
                if(map[i][j] == '.')
                {
                    if(ans)
                    {
                        if(!cnt)
                            printf("%d",ans);
                        else
                            printf(" %d",ans);
                        cnt++;
                    }
                    ans = 0;
                }
                else if(map[i][j] == 'X')
                    ans++;
            }
            if(ans)
            {
                if(cnt)
                    printf(" %d",ans);
                else
                    printf("%d",ans);
            }
            else if(!cnt)
                printf("0");
            printf("\n");
        }
        for(j = 0; j<n; j++)
        {
            ans = 0;
            int cnt = 0;
            for(i = 0; i<n; i++)
            {
                if(map[i][j] == '.')
                {
                    if(ans)
                    {
                        if(!cnt)
                            printf("%d",ans);
                        else
                            printf(" %d",ans);
                        cnt++;
                    }
                    ans = 0;
                }
                else if(map[i][j] == 'X')
                    ans++;
            }
            if(ans)
            {
                if(cnt)
                    printf(" %d",ans);
                else
                    printf("%d",ans);
            }
            else if(!cnt)
                printf("0");
            printf("\n");
        }
    }

    return 0;
}


 

你可能感兴趣的:(HUNNU11398:Reverse Nonogram)