Rings

Rings
Dee Siduous is a botanist who specializes in trees. A lot of her research has to do with the formation of
tree rings, and what they say about the growing conditions over the tree’s lifetime. She has a certain
theory and wants to run some simulations to see if it holds up to the evidence gathered in the field.
One thing that needs to be done is to determine the expected number of rings given the outline of
a tree. Dee has decided to model a cross section of a tree on a two dimenional grid, with the interior
of the tree represented by a closed polygon of grid squares. Given this set of squares, she assigns rings
from the outer parts of the tree to the inner as follows: calling the non-tree grid squares “ring 0”, each
ring n is made up of all those grid squares that have at least one ring (n − 1) square as a neighbor
(where neighboring squares are those that share an edge).
An example of this is shown in the figure below.

Most of Dee’s models have been drawn on graph paper, and she has come to you to write a program
to do this automatically for her. This way she’ll use less paper and save some … well, you know.

Input
The input file contains several test cases, each of them as described below.
The input will start with a line containing two positive integers n m specifying the number of rows
and columns in the tree grid, where n, m ≤ 100. After this will be n rows containing m characters each.
These characters will be either ‘T’ indicating a tree grid square, or ‘.’.
Output
For each test case, the output must follow the description below.
Output a grid with the ring numbers. If the number of rings is less than 10, use two characters for
each grid square; otherwise use three characters for each grid square. Right justify all ring numbers in
the grid squares, and use ‘.’ to fill in the remaining characters.
If a row or column does not contain a ring number it should still be output, filled entirely with ‘.’s.

題目鏈接:http://115.28.83.210/jnuoj/contest_show.php?cid=7#problem/D

嗯,這是個水題
題目說是年輪,其實就是從外到內,一圈一圈從小到大填數字。
用數組存儲網格
最外層只要和“.”相接觸就是1。
然後往內層,只要和1接觸就是2,以此類推。
我做的時候在把邊界全部存儲爲0,相當於重新做了一個邊界。
這樣在網格里面全是T的時候便於判斷最外面的1。
自己做的時候,內存開小了,只開了100,WA了好多次,心痛。
這是個教訓,以後多開點。。。
哦對,還要注意三位數的時候和兩位數輸出格式不同。

#include<iostream>
#include<stdio.h>
using namespace std;
int i, j, k;
int x, y, counts;
int map[102][102];
char temp;
int main()
{
    while (cin >> x >> y)
    {
        for (i = 0; i < 102; i++)
            for (j = 0; j < 102; j++)
                map[i][j] = 0;
        for (i = 1; i <= x; i++)
        {
            for (j = 1; j <= y; j++)
            {
                cin >> temp;
                if (temp == '.')
                    map[i][j] = 0;
                else if (temp == 'T')
                    map[i][j] = -1;
            }
        }
        for (i = x; i >0; i--)//掃網格,找出1
        {
            for (j = y; j >0; j--)
            {
                if (map[i][j] == -1)
                    if (map[i + 1][j] == 0 || map[i - 1][j] == 0 || map[i][j + 1] == 0 || map[i][j - 1] == 0)
                        map[i][j] = 1;
            }
        }
        counts = 1;//標記上一次找的數字
        for (k = 1;counts!=0; k++)//掃網格判斷2,3,4……
        {
            for (counts=0,i = x; i >0; i--)
                for (j = y; j >0; j--)
                {
                    if (map[i][j] == -1)
                    {
                        counts++;
                        if (map[i + 1][j] == k || map[i - 1][j] == k || map[i][j + 1] == k || map[i][j - 1] == k)
                        {
                            map[i][j] = k + 1;
                        }
                    }
                }
        }
        for (i = 1; i <= x; i++)
        {
            for (j = 1; j <= y; j++)
            {
                if (map[i][j] == 0)
                {
                    if (k > 10)
                        printf("...");
                    else
                        printf("..");
                }
                else {
                    if (k > 10)
                    {
                        if (map[i][j] < 10)
                            printf("..%d", map[i][j]);
                        else
                            printf(".%d", map[i][j]);
                    }
                    else
                        printf(".%d", map[i][j]);
                    }
            }
            printf("\n");
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章