UVa379 - Hi-Q(模拟)

Hi-Q is a popular solitaire game that comes in a small box with aplaying board that has little holes in the shape of a cross and 32little pegs that fit into the holes. Starting with the centermost holeopen, players move the pegs by jumping one peg over another,either in a horizontal or vertical direction and removing each pegthat is jumped over. Diagonal jumps are not allowed. The objectfor players is to remove as many pegs from the board as possible. This problem involves writing a program that will automaticallyplay Hi-Q so that we can investigate how the game might unfoldbased on various opening arrangements of pegs.

There is a peg board with the followingshape and with holes numbered from 1 to 33 as follows:

                              1         2         3
                              4         5         6
          7         8         9        10        11        12        13
         14        15        16        17        18        19        20
         21        22        23        24        25        26        27        
                             28        29        30
                             31        32        33

An instance of the game begins with some holes having pegs inthem and the rest of the holes being empty. The game proceeds byjumping one peg over another, either horizontally or vertically,with the peg that is jumping landing in an empty hole, and the pegbeing jumped over being removed from the board. For example, if9 is empty and 10 and 11 are not, then the peg in 11 can be``moved" to 9 with the peg in 10 being removed. After this move,10 and 11 would both be empty but 9 would have a peg in it.

Given a specific board configuration your program will pick andmodel a specific move, over and over, until no more moves areavailable. Your program will then report the sum of the holes thatstill have pegs in them. At any point during the game there may bemore than one possible move available. In such a case alwaysmodel the move with the target hole of the moving peg as large aspossible. If there is more than one move available to the largestpossible target hole, then choose from those moves the one withthe larger source hole.

For example, if the board looks like this, with X representing a pegandO representing a hole:

                              O         O         O
                              O         O         O
          O         O         O         X         O         X         O
          O         O         O         X         O         X         O
          O         O         O         O         X         O         O
                              O         O         O
                              O         O         O

then the following jumps would be made: 1: from 12 over 19 to 26(26, 24, and 5 are the only possible targets and 26 is the largest), 2:from 26 to 24 over 25 (5 and 24 are the only possible targets with24 > 5 plus 24 is the target for two possible moves, one from 26and one from 10; the one from 26 is used since 26 > 10), 3: from17 to 29 (29 > 5), and two pegs would be left, one in hole 10 andone in hole 29. Thus 39 would be reported as the result for thisinstance.

NOTE: The above paragraph is wrong. This was discovered shortly after the contest began, and the correction was broadcast to all teams. The second jump should be from 25 to 27. The third jump should be from 10 to 24. The two pegs left will be in holes 24 and 27 and 51 should be reported as the result.

Input

The first line contains an integer N between 1 and 10describing how many instances of the game are represented. Theremaining lines will describeN instances of the game by listing theholes which begin with pegs in them, in increasing order. A 0 willindicate the end of each sequence of unique numbers between 1and 33 that represents an instance of the game.

Output

There should be N+2 lines of output. The first line ofoutput will readHI Q OUTPUT. There will then be one line ofoutput for each instance of the game, reporting the sum of the holesthat still have pegs in them for the final configuration of thatinstance.. The final line of output should readEND OF OUTPUT.

Sample Input

4
10 12 17 19 25 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25 26   27 28 29 30 31 32 33 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 0

Sample Output

HI Q OUTPUT
51                                                          
0
561  
98
END OF OUTPUT
题意:peg移动方式为水平方向、竖起方向,只能跨过一个跳到空格的地方,有多种情况时,选择目的序号大的跳转方式,如果有两种方式跳转的目的序号是一样的,则选择起始序号最大的移动方式。这题是仿真问题

#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
#include <map>

using namespace std;

const int MAXN = 7;

struct Node
{
	int src, dst;
	char dir;

	bool operator < (const Node &other) const
	{
		if (dst != other.dst) return dst < other.dst;
		
		return src < other.src;
	}
};

int grid[MAXN][MAXN];
map< int, pair<int, int> > numMap;
map<pair<int,int>, int> posMap;

void init()
{
	pair<int,int> p;
	int num = 1;

	for (int i = 0; i < 2; i++) {
		for (int j = 2; j < 5; j++) {
			p = make_pair(i, j);
			numMap[num] = p;
			posMap[p] = num++;
		}
	}

	for (int i = 2; i < 5; i++) {
		for (int j = 0; j < 7; j++) {
			p = make_pair(i, j);
			numMap[num] = p;
			posMap[p] = num++;
		}
	}

	for (int i = 5; i < 7; i++) {
		for (int j = 2; j < 5; j++) {
			p = make_pair(i, j);
			numMap[num] = p;
			posMap[p] = num++;
		}
	}
	
}

void input()
{
	memset(grid, -1, sizeof(grid));
	for (map< int, pair<int,int> >::const_iterator it = numMap.begin(); it != 
numMap.end(); it++) {
		pair<int,int> p = it->second;
		int x = p.first, y = p.second;
		grid[x][y] = 0;
	}

	while (1) {
		int num;
		scanf("%d", &num);
		if (num == 0) break;
		pair<int, int> p = numMap[num];
		grid[p.first][p.second] = 1;
	}
}

void solve()
{
	while (1) {
		priority_queue<Node> q;

		for (int i = 0; i < MAXN; i++) {
			for (int j = 0; j < MAXN; j++) {
				if (grid[i][j] != -1) {
					if (grid[i][j] == 0 && j + 1 < MAXN && grid[i][j + 1] == 1 && j + 2 < 
MAXN && grid[i][j + 2] == 1) {
						Node tmp;
						tmp.src = posMap[pair<int,int>(i, j + 2)]; tmp.dst = posMap[pair<int, 
int>(i, j)];tmp.dir = 'h';
						q.push(tmp);
					} else if (grid[i][j] == 1 && j + 1 < MAXN && grid[i][j + 1] == 1 && j + 
2 < MAXN && grid[i][j + 2] == 0) {
						Node tmp;
						tmp.src = posMap[pair<int,int>(i, j)]; tmp.dst = posMap[pair<int,int>(i
, j + 2)]; tmp.dir = 'h';
						q.push(tmp);
					}

					if (grid[i][j] == 0 && i + 1 < MAXN && grid[i + 1][j] == 1 && i + 2 < 
MAXN && grid[i + 2][j] == 1) {
						Node tmp;
						tmp.src = posMap[pair<int,int>(i + 2, j)]; tmp.dst = posMap[pair<int,int
>(i, j)]; tmp.dir = 'v';
						q.push(tmp);
					} else if (grid[i][j] == 1 && i + 1 < MAXN && grid[i + 1][j] == 1 && i + 
2 < MAXN && grid[i + 2][j] == 0) {
						Node tmp;
						tmp.src = posMap[pair<int,int>(i, j)]; tmp.dst = posMap[pair<int,int>(i 
+ 2, j)]; tmp.dir = 'v';
						q.push(tmp);
					}
				}
			}
		}

		if (q.empty()) break;
		Node tmp = q.top(); q.pop();
		if (tmp.src < tmp.dst && tmp.dir == 'h') {
			pair<int,int> p = numMap[tmp.src];
			int x = p.first, y = p.second;
			grid[x][y] = 0;
			grid[x][y + 1] = 0;
			grid[x][y + 2] = 1;
		} else if (tmp.src > tmp.dst && tmp.dir == 'h') {
			pair<int,int> p = numMap[tmp.src];
			int x = p.first, y = p.second;
			grid[x][y] = 0;
			grid[x][y - 1] = 0;
			grid[x][y - 2] = 1;
		} else if (tmp.src < tmp.dst && tmp.dir == 'v') {
			pair<int,int> p = numMap[tmp.src];
			int x = p.first, y = p.second;
			grid[x][y] = 0;
			grid[x + 1][y] = 0;
			grid[x + 2][y] = 1;
		} else if (tmp.src > tmp.dst && tmp.dir == 'v') {
			pair<int,int> p = numMap[tmp.src];
			int x = p.first, y = p.second;
			grid[x][y] = 0;
			grid[x - 1][y] = 0;
			grid[x - 2][y] = 1;
		}

		while (!q.empty()) q.pop();
	}

	int ans = 0;
	for (int i = 0; i < MAXN; i++) {
		for (int j = 0; j < MAXN; j++) {
			if (grid[i][j] == 1) {
				ans += posMap[make_pair(i, j)];
			}
		}
	}

	printf("%d\n", ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	init();

	int cas;
	scanf("%d", &cas);
	printf("HI Q OUTPUT\n");
	while (cas--) {
		input();
		solve();
	}
	printf("END OF OUTPUT\n");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值