acm pku 1132 Border的具体实现
来源:互联网 发布:短线高手指标源码 编辑:程序博客网 时间:2024/05/22 04:27
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 <<endl;
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
- acm pku 1132 Border的具体实现
- acm pku 1107 W's Cipher的具体实现方法
- acm pku 1118 Lining Up的具体实现方法
- acm pku 1126 Simple Syntax的具体实现
- acm pku 1068 Parencodings的实现
- acm pku 1056 IMMEDIATE DECODABILITY的代码实现
- acm pku 1125 Stockbroker Grapevine的Dijkstra算法实现
- acm pku 1142 Smith Numberd的模拟实现方法
- acm pku 1226 Substring的算法分析与实现
- acm pku 1247 Magnificent Meatballs的算法实现
- acm pku 1251 Jungle Roads的算法分析与实现
- acm pku 1258 Agri-Net的Prim算法实现
- acm pku 1260 Pearls的动态规划算法实现
- acm pku 1298 The hardest problem ever的实现
- acm pku 1230Pass-Muraille的贪婪算法实现
- acm pku 1299 Polar Exploer的简单实现
- PKU ACM 1002 487-3279 C++实现
- pku-acm
- 七个受用一生的心理寓言
- 前端开发大众手册(包括工具、网址、经验等)
- 如何在代码中重启手机
- Qt定时器QTimer的类定义与例子及代码详解
- 调试客户端脚本 利用VS.Net 中的调试器来调试JavaScript
- acm pku 1132 Border的具体实现
- Delta3D引擎
- 优秀程序员45种习惯
- 常见的开源游戏引擎
- c++初始化成员列表
- 再说:Vector、ArrayList、List使用与区别
- 基于Linux的便携嵌入式设备电源管理解决方案
- 出差,心情讲述
- Android中如何修改系统时间(应用程序获得系统权限)