UVa 114 Simulation Wizardry

難度不高,規則一堆。必須很細心去應付的題目,光是題目敘述就落落長了。
唯一的小陷阱是:不管有沒有撞到牆,每一步ball life都要減一。
剛剛一看Last Modified才發現這題從開始到結束拖了兩個月之久...我的媽呀
繼續朝著連號解題之路邁進 100~114

/**
* UVa 114 Simulation Wizardry
* Author: chchwy
* Last Modified: 2009.02.03
*/
#include<iostream>
using namespace std;
enum
{
MAX_SIZE = 51 + 2,
SPACE = 0, WALL = 1, BUMPER = 2,
};
// 0=> 朝x方向增加 1=> 朝y方向增加
// 2=> 朝x方向減小 3=> 朝y方向減小
const int move[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
class Grid
{
public :
int type;
int cost;
int value;
void set (int ptype, int pvalue, int pcost )
{
type = ptype; value = pvalue; cost = pcost;
}
};
int run(Grid map[MAX_SIZE][MAX_SIZE], int x, int y, int direction, int life )
{
int point = 0;
while ( life > 1 )
{
life -= 1;
int nextX = x + move[ direction ][0];
int nextY = y + move[ direction ][1];
switch ( map[nextY][nextX].type )
{
case WALL:
case BUMPER:
life -= map[nextY][nextX].cost;
point += map[nextY][nextX].value;
direction = (direction + 3) % 4; //turn right;
break;
case SPACE:
x = nextX, y = nextY;
break;
}
}
return point;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("114.in", "r", stdin);
#endif
/* surface size */
int m, n;
scanf("%d %d", &m, &n);
/* map surface */
Grid map[MAX_SIZE][MAX_SIZE];
memset(map, 0, sizeof(map));
/* set wall around the surface */
int costWall;
scanf("%d", &costWall);
for (int i = 1; i <= m; ++i)
{
map[i][1].set(WALL, 0, costWall);
map[i][n].set(WALL, 0, costWall);
}
for (int i = 1; i <= n; ++i)
{
map[1][i].set(WALL, 0, costWall);
map[m][i].set(WALL, 0, costWall);
}
/* init bumper */
int numBumper;
scanf("%d", &numBumper);
for (int i = 0; i < numBumper; ++i)
{
int x, y, value, cost;
scanf("%d %d %d %d", &x, &y, &value, &cost);
map[y][x].set(BUMPER, value, cost);
}
/* start simulation */
int x, y, direction, life;
int totalPoint = 0;
//剩下每一行都代表一顆球 ( x, y, 方向, 生命值)
while ( scanf("%d%d%d%d", &x, &y, &direction, &life) == 4 )
{
int point = run(map, x, y, direction, life);
printf("%d\n", point );
totalPoint += point;
}
printf("%d\n", totalPoint);
return 0;
}
view raw 114.cpp hosted with ❤ by GitHub

留言

  1. 想請問一下UVA 10676該如何解
    實在很難決定適當的量尺
    這題困了我好幾個月

    回覆刪除

張貼留言

這個網誌中的熱門文章

UVa 10125 Sumsets

讀書心得: 撒哈拉的故事

讀書心得: 你以為你以為的就是你以為的嗎?