24h購物| | PChome| 登入
2013-08-23 11:53:54| 人氣1,780| 回應0 | 上一篇 | 下一篇

[UVA] 12155 - ASCII Diamondi

推薦 0 收藏 0 轉貼0 訂閱站台

 

ASCII diamonds can be drawn with integer side lengths. Each layer of this diamond is drawn with a single ASCII alphabet, starting with ‘a’ and ending with ‘z’ (starting from the center) and continues in cyclic order.

Figure 1: ASCII diamond for different side lengths.

 

Any one of these ASCII diamonds can be used to draw an infinite plane by using this as a tile. For example ASCII diamond of length 5 can be used to draw such an infinite grid. Only first 20 row and 60 columns are shown below:

 

Here rows and columns are numbered starting from zero. By specifying the topmost row (row1), leftmost column (col1), bottommost row (row2) and rightmost column (col2) we can specify a portion of such an infinite grid (also shown in figure above).

 

Given the side length of the tile to be used, the topmost row (row1), leftmost column (col1), bottommost row (row2) and rightmost column (col2) you have to print the pattern within these four boundaries (inclusive).

  

Input

Input contains at most 125 sets of inputs. But not all cases are extreme.

 

Each set of input contains five integers: N (0<N£20000), row1, col1, row2, col2 (0£ row1 £ row2 £ 20000, 0 £ col1 £ col2 £ 20000, 0£ (row2- row1+1)* (col2- col1+1) £ 40000). Here N denotes that the side length of the tiles used to draw the plane should be N. The meaning of row1, col1, row2, col2 are given in the problem statement. The first sample input corresponds to the figure above.

 

Input is terminated by a line where the first integer is zero.

 

Output

For each line of input produce (row2- row1+2) lines of output. First line contains serial of output. Each of the next lines contain (col2- col1+1) characters. These lines describe the patterns within the specified rows and columns. Look at the output for sample input for details. The output file size is less than 1 MB.

 

Sample Input                              Output for Sample Input

5 3 18 10 46

100 50 50 69 69

0 2 3 4 5

 

Case 1:

.edcbcde..edcbcde..edcbcde..e

edcbabcdeedcbabcdeedcbabcdeed

.edcbcde..edcbcde..edcbcde..e

..edcde....edcde....edcde....

...ede......ede......ede.....

....e........e........e......

....e........e........e......

...ede......ede......ede.....

Case 2:

utsrqponmlkjihgfedcb

tsrqponmlkjihgfedcba

srqponmlkjihgfedcbaz

rqponmlkjihgfedcbazy

qponmlkjihgfedcbazyx

ponmlkjihgfedcbazyxw

onmlkjihgfedcbazyxwv

nmlkjihgfedcbazyxwvu

mlkjihgfedcbazyxwvut

lkjihgfedcbazyxwvuts

kjihgfedcbazyxwvutsr

jihgfedcbazyxwvutsrq

ihgfedcbazyxwvutsrqp

hgfedcbazyxwvutsrqpo

gfedcbazyxwvutsrqpon

fedcbazyxwvutsrqponm

edcbazyxwvutsrqponml

dcbazyxwvutsrqponmlk

cbazyxwvutsrqponmlkj

bazyxwvutsrqponmlkji


Problem setter: Shahriar Manzoor, Special Thanks: Sohel Hafiz, Md. Arifuzzaman Arif

 

考慮將座標映射到左上角的菱形,然後再轉換到菱形的左上角部分(切成四等份的左上角)。

最後用數學計算,決定要輸出哪個字元。


#include <stdio.h>
#include <stdlib.h>
int main() {
    int cases = 0;
    int n, row1, row2, col1, col2;
    while(scanf("%d %d %d %d %d", &n, &row1, &col1, &row2, &col2) == 5) {
        if(n == 0)
            break;
        printf("Case %d:\n", ++cases);
        int i, j, k;
        int width = 2*n-1;
        for(i = row1; i <= row2; i++) {
            for(j = col1; j <= col2; j++) {
                int x = i%width;
                int y = j%width;
                // mapped to left upper
                if(x >= n)
                    x = n-1 - (x-(n-1));
                if(y >= n)
                    y = n-1 - (y-(n-1));
                if(x + y >= n-1) {
                    int v = (abs(x-(n-1))+abs(y-(n-1)))%26;
                    putchar(v+'a');
                } else {
                    putchar('.');
                }

            }
            puts("");
        }
    }
    return 0;
}

台長: Morris
人氣(1,780) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 912 - Live From Mars
此分類上一篇:[UVA][字串] 10875 - Big Math

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文