UVa Problem 114 - Simulation Wizardry

  1. // UVa Problem 114 - Simulation Wizardry   
  2. // Verdict: Accepted   
  3. // Submission Date: 2011-12-02   
  4. // UVa Run Time: 0.084s   
  5. //   
  6. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  7. //   
  8. // [解题方法]   
  9. // 纯粹模拟题,注意细节即可。   
  10.   
  11. #include <iostream>   
  12.   
  13. using namespace std;  
  14.   
  15. #define MAXN 55   
  16.   
  17. #define GRID 0   
  18. #define OBSTACLE 1   
  19. #define WALL 3   
  20.   
  21. #define RIGHT 0   
  22. #define UP 1   
  23. #define LEFT 2   
  24. #define DOWN 3   
  25.   
  26. struct obstacle  
  27. {  
  28.     int value, cost;  
  29. };  
  30.   
  31. struct ball  
  32. {  
  33.     int x, y, direction, lifetime, points;  
  34. };  
  35.   
  36. ball pinball;  
  37. obstacle bumpers[MAXN][MAXN];  
  38. int grid[MAXN][MAXN], gridWidth, gridHeight, costHitWall;  
  39. int offset[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };  
  40.   
  41. void ballStatus(void)  
  42. {  
  43.     cout << pinball.x << " " << pinball.y << " " << pinball.direction;  
  44.     cout << " " << pinball.lifetime << " " << pinball.points << endl;  
  45. }  
  46.   
  47. void play(void)  
  48. {  
  49.     int newX, newY;  
  50.       
  51.     while (pinball.lifetime > 0)  
  52.     {  
  53.         // ballStatus();   
  54.   
  55.         pinball.lifetime--;  
  56.         newX = pinball.x + offset[pinball.direction][0];  
  57.         newY = pinball.y + offset[pinball.direction][1];  
  58.   
  59.         if (grid[newY][newX] == WALL)  
  60.         {  
  61.             if (newY == gridHeight && pinball.direction == UP ||  
  62.                 newY == 1 && pinball.direction == DOWN ||  
  63.                 newX == gridWidth && pinball.direction == RIGHT ||  
  64.                 newX == 1 && pinball.direction == LEFT)  
  65.             {  
  66.                 pinball.direction--;  
  67.                 if (pinball.direction < 0)  
  68.                     pinball.direction += 4;  
  69.                 pinball.lifetime -= costHitWall;      
  70.             }             
  71.         }  
  72.         else if (grid[newY][newX] == OBSTACLE)  
  73.         {  
  74.             pinball.direction--;  
  75.             if (pinball.direction < 0)  
  76.                 pinball.direction += 4;  
  77.             if (pinball.lifetime > 0)  
  78.             {  
  79.                 pinball.points += bumpers[newY][newX].value;  
  80.                 pinball.lifetime -= bumpers[newY][newX].cost;  
  81.             }  
  82.         }  
  83.         else  
  84.         {  
  85.             pinball.x = newX;  
  86.             pinball.y = newY;  
  87.         }  
  88.     }  
  89. }  
  90.   
  91. int main (int argc, char const* argv[])  
  92. {  
  93.     int totalPoints = 0, numberBumpers, tmpX, tmpY;  
  94.   
  95.     cin >> gridWidth >> gridHeight;  
  96.     cin >> costHitWall;  
  97.     for (int i = 1; i <= gridHeight; i++)  
  98.         for (int j = 1; j <= gridWidth; j++)  
  99.             if (i == 1 || j == 1 || i == gridHeight || j == gridWidth)  
  100.                 grid[i][j] = WALL;  
  101.             else  
  102.                 grid[i][j] = GRID;  
  103.   
  104.     cin >> numberBumpers;  
  105.     for (int i = 1; i <= numberBumpers; i++)  
  106.     {  
  107.         cin >> tmpX >> tmpY;  
  108.         grid[tmpY][tmpX] = OBSTACLE;  
  109.         cin >> bumpers[tmpY][tmpX].value;  
  110.         cin >> bumpers[tmpY][tmpX].cost;  
  111.     }  
  112.   
  113.     while (cin >> pinball.x)  
  114.     {  
  115.         cin >> pinball.y >> pinball.direction >> pinball.lifetime;  
  116.         pinball.points = 0;  
  117.           
  118.         play();  
  119.       
  120.         totalPoints += pinball.points;  
  121.         cout << pinball.points << endl;  
  122.     }  
  123.   
  124.     cout << totalPoints << endl;  
  125.   
  126.     return 0;  
  127. }  
// UVa Problem 114 - Simulation Wizardry
// Verdict: Accepted
// Submission Date: 2011-12-02
// UVa Run Time: 0.084s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 纯粹模拟题,注意细节即可。

#include <iostream>

using namespace std;

#define MAXN 55

#define GRID 0
#define OBSTACLE 1
#define WALL 3

#define RIGHT 0
#define UP 1
#define LEFT 2
#define DOWN 3

struct obstacle
{
	int value, cost;
};

struct ball
{
	int x, y, direction, lifetime, points;
};

ball pinball;
obstacle bumpers[MAXN][MAXN];
int grid[MAXN][MAXN], gridWidth, gridHeight, costHitWall;
int offset[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };

void ballStatus(void)
{
	cout << pinball.x << " " << pinball.y << " " << pinball.direction;
	cout << " " << pinball.lifetime << " " << pinball.points << endl;
}

void play(void)
{
	int newX, newY;
	
	while (pinball.lifetime > 0)
	{
		// ballStatus();

		pinball.lifetime--;
		newX = pinball.x + offset[pinball.direction][0];
		newY = pinball.y + offset[pinball.direction][1];

		if (grid[newY][newX] == WALL)
		{
			if (newY == gridHeight && pinball.direction == UP ||
				newY == 1 && pinball.direction == DOWN ||
				newX == gridWidth && pinball.direction == RIGHT ||
				newX == 1 && pinball.direction == LEFT)
			{
				pinball.direction--;
				if (pinball.direction < 0)
					pinball.direction += 4;
				pinball.lifetime -= costHitWall;	
			}			
		}
		else if (grid[newY][newX] == OBSTACLE)
		{
			pinball.direction--;
			if (pinball.direction < 0)
				pinball.direction += 4;
			if (pinball.lifetime > 0)
			{
				pinball.points += bumpers[newY][newX].value;
				pinball.lifetime -= bumpers[newY][newX].cost;
			}
		}
		else
		{
			pinball.x = newX;
			pinball.y = newY;
		}
	}
}

int main (int argc, char const* argv[])
{
	int totalPoints = 0, numberBumpers, tmpX, tmpY;

	cin >> gridWidth >> gridHeight;
	cin >> costHitWall;
	for (int i = 1; i <= gridHeight; i++)
		for (int j = 1; j <= gridWidth; j++)
			if (i == 1 || j == 1 || i == gridHeight || j == gridWidth)
				grid[i][j] = WALL;
			else
				grid[i][j] = GRID;

	cin >> numberBumpers;
	for (int i = 1; i <= numberBumpers; i++)
	{
		cin >> tmpX >> tmpY;
		grid[tmpY][tmpX] = OBSTACLE;
		cin >> bumpers[tmpY][tmpX].value;
		cin >> bumpers[tmpY][tmpX].cost;
	}

	while (cin >> pinball.x)
	{
		cin >> pinball.y >> pinball.direction >> pinball.lifetime;
		pinball.points = 0;
		
		play();
	
		totalPoints += pinball.points;
		cout << pinball.points << endl;
	}

	cout << totalPoints << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值