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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值