Border
Description
You are to write a program that draws a border around a closed path into a bitmap, as displayed in the following figure:
The path is closed and runs along the grid lines, i.e. between the squares of the grid. The path runs counter-clockwise, so if following the path is considered as going ``forward'', the border pixels are always to the "right'' of the path. The bitmap always covers 32 by 32 squares and has its lower left corner at (0, 0). You can safely assume that the path never touches the bounding rectangle of the bitmap and never touches or crosses itself. Note that a bit gets set if it is on the outside of the area surrounded by the path and if at least one of its edges belongs to the path, but not if only one of its corners is in the path. (A look at the convex corners in the figure should clarify that statement.)
Input
The first line of the input file contains the number of test cases in the file. Each test case that follows consists of two lines. The first line of each case contains two integer numbers x and y specifying the starting point of the path. The second line contains a string of variable length. Every letter in the string symbolizes a move of length one along the grid. Only the letters 'W' ("west"), 'E' ("east"), 'N' ("north"), 'S' ("south"), and '.' ("end of path", no move) appear in the string. The end-of-path character ( '.') is immediately followed by the end of the line.
Output
For each test case, output a line with the number of the case ('Bitmap #1', 'Bitmap #2', etc.). For each row of the bitmap from top to bottom, print a line where you print a character for every bit in that row from left to right. Print an uppercase 'X' for set bits and a period '.' for unset bits. Output a blank line after each bitmap.
Sample Input
1
2 1
EENNWNENWWWSSSES.
Sample Output
Bitmap #1
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
.XXX............................
X...X...........................
X..X............................
X...X...........................
.X..X...........................
..XX............................
Source
Southwestern European Regional Contest 1996
这是一道较为简单的模拟题,只要能够模拟出格子移动的方向,就可以得到正确答案。对作者而言,在编程过程中遇到的主要问题如下:
1, 按要求表移动方向的字母应是大写字母,在编程中,不小心写成了小写,WA两次,不不值了;
2, 对其实位置的判断是必须的,因为对任意一个点来讲,其起始位置面向东、南、西、北各方时,有不同的起始输出(MoveGrid函数的初始化部分);
3, 在移动过程中,如果方向突然改变,则也应做相应的处理(MoveGrid函数的Switch部分)。
具体实现:
#include "iostream"
using namespace std;
const int N = 32;
bool bstr[N][N];
int idrt[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void MoveGrid(int row, int col, char *cDirct)
{
int i, tmpdirct[2];
if(cDirct[0] == 'E')
{
tmpdirct[0] = N - col;
tmpdirct[1] = row;
}
else if(cDirct[0] == 'S')
{
tmpdirct[0] = N - col;
tmpdirct[1] = row - 1;
}
else if(cDirct[0] == 'W')
{
tmpdirct[0] = N - col - 1;
tmpdirct[1] = row - 1;
}
else if(cDirct[0] == 'N')
{
tmpdirct[0] = N - col - 1;
tmpdirct[1] = row;
}
bstr[tmpdirct[0]][tmpdirct[1]] = true;
for(i = 1; i < strlen(cDirct); i++)
{
if(cDirct[i] == '.') break;
switch(cDirct[i])
{
case 'E':
tmpdirct[0] += idrt[0][0];
tmpdirct[1] += idrt[0][1];
if(cDirct[i-1] == 'N') tmpdirct[1] -= 1;
else if(cDirct[i-1] == 'S') tmpdirct[0] += 1;
break;
case 'S':
tmpdirct[0] += idrt[1][0];
tmpdirct[1] += idrt[1][1];
if(cDirct[i-1] == 'E') tmpdirct[0] -= 1;
else if(cDirct[i-1] == 'W') tmpdirct[1] -= 1;
break;
case 'W':
tmpdirct[0] += idrt[2][0];
tmpdirct[1] += idrt[2][1];
//if(cDirct[i-1] == 's') tmpdirct[1] += 1; // WA 2times:字母's'是小写的,不能为小写
if(cDirct[i-1] == 'S') tmpdirct[1] += 1;
else if(cDirct[i-1] == 'N') tmpdirct[0] -= 1;
break;
case 'N':
tmpdirct[0] += idrt[3][0];
tmpdirct[1] += idrt[3][1];
if(cDirct[i-1] == 'E') tmpdirct[1] += 1;
else if(cDirct[i-1] == 'W') tmpdirct[0] += 1;
break;
default:
break;
}
bstr[tmpdirct[0]][tmpdirct[1]] = true;
}
}
int main(void)
{
int n, r, c, num = 0;
int i, j;
char cDirct[N*N];
cin >> n;
while(n-- > 0)
{
cin >> r >> c;
cin >> cDirct;
for(i = 0; i < N; i++)
memset(bstr[i], 0, sizeof(bool)*N);
num ++;
cout << "Bitmap #" << num <
MoveGrid(r, c, cDirct);
cDirct[0] = '/0';
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
if(bstr[i][j]) cout << "X";
else cout << ".";
}
cout << endl;
}
cout << endl;
}
return 0;
}
执行结果:
Problem: 1132 |
|
User: uestcshe |
Memory: 208K |
|
Time: 0MS |
Language: C++ |
|
Result: Accepted |