ACM-ICPC2018亚洲区预选赛北京站网络赛部分题解

A.Saving Tang Monk II【BFS】
题目链接:https://cn.vjudge.net/problem/HihoCoder-1828

#include 
#include 
#include 
#include 
using namespace std;
static const int MAXN = 100;
static const int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
char mp[MAXN + 10][MAXN + 10];
bool vis[6][MAXN + 10][MAXN + 10];
int n, m;
struct Node {
	int x, y, step, bottle;
	Node(int x, int y, int step, int bottle) :x(x), y(y), step(step), bottle(bottle) {};
	bool operator<(Node a)const {
		return step > a.step;
	}
};
void bfs(Node node)
{
	priority_queue<Node> Q;
	Q.push(node);
	vis[node.bottle][node.x][node.y] = true;
	while(!Q.empty())
	{
		Node now = Q.top();
		Q.pop();
		for (int i = 0; i < 4; i++)
		{
			Node next = now;
			next.x += dx[i], next.y += dy[i];
			if (1 <= next.x && next.x <= n && 1 <= next.y && next.y <= m && !vis[next.bottle][next.x][next.y])
			{
				vis[next.bottle][next.x][next.y] = true;
				if (mp[next.x][next.y] == 'T')
				{
					cout << next.step + 1 << endl;
					return;
				}
				else if (mp[next.x][next.y] == '.' || mp[next.x][next.y] == 'S')
				{
					next.step += 1;
					Q.push(next);
				}
				else if (mp[next.x][next.y] == '#' && next.bottle)
				{
					next.bottle -= 1;
					next.step += 2;
					Q.push(next);
				}
				else if (mp[next.x][next.y] == 'B')
				{
					if (next.bottle < 5)
						next.bottle += 1;
					next.step += 1;
					Q.push(next);
				}
				else if (mp[next.x][next.y] == 'P')
					Q.push(next);
			}
		}
	}
	cout << -1 << endl;
	return;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), cout.tie(0);
	while (cin >> n >> m && n && m)
	{
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				cin >> mp[i][j];
		memset(vis, false, sizeof(vis));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				if (mp[i][j] == 'S')
					bfs(Node{ i,j,0,0 });
	}
	return 0;
}

B.Tomb Raider【暴力】【set】
题目链接:https://cn.vjudge.net/problem/HihoCoder-1829

#include 
#include 
#include 
#include 
#include 
using namespace std;
string st[11];
set<string> se[11];
int n;
void subset(string s, int len, int num)
{
	for (int i = 0; i <= (1 << len) - 1; i++)
	{
		string ss;
		for (int j = 0; j < len; j++)
		{
			if (((i >> j) & 1) == 1)
				ss += s[j];
		}
		if (ss.size())
			se[num].insert(ss);
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), cout.tie(0);
	while (cin >> n && n)
	{
		for (int i = 0; i < n; i++)
		{
			se[i].clear();
			cin >> st[i];
			int len = st[i].size();
			st[i] += st[i];
			for (int j = 0; j < len; j++)
			{
				string s(st[i].begin() + j, st[i].begin() + len + j);
				subset(s, len, i);
			}
		}
		vector<string> v;
		for (string ss : se[0])
		{
			bool flag = true;
			for (int i = 1; i < n; i++)
			{
				if (se[i].count(ss) == 0)
				{
					flag = false;
					break;
				}
			}
			if (flag)
				v.push_back(ss);
		}
		if (v.size() == 0)
		{
			cout << 0 << endl;
			continue;
		}
		sort(v.begin(), v.end());
		string ans;
		int maxlen = 0;
		for (string ss : v)
		{
			if (ss.size() > maxlen)
			{
				ans = ss;
				maxlen = ss.size();
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C.Cheat【模拟】
题目链接:https://cn.vjudge.net/problem/HihoCoder-1830

#include 
#include 
#include 
using namespace std;
int poke[5][14], hand[5], table[14];
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), cout.tie(0);
	char ch;
	while (cin >> ch)
	{
		memset(poke, 0, sizeof(poke));
		memset(table, 0, sizeof(table));
		if (ch == 'A') poke[1][1]++;
		else if ('2' <= ch && ch <= '9') poke[1][ch - '0']++;
		else if (ch == '1') { cin >> ch; poke[1][10]++; }
		else if (ch == 'J') poke[1][11]++;
		else if (ch == 'Q') poke[1][12]++;
		else if (ch == 'K') poke[1][13]++;
		for (int i = 1; i <= 4; i++)
			for (int j = 1; j <= 13; j++)
			{
				if (i == 1 && j == 1) continue;
				cin >> ch;
				if (ch == 'A') poke[i][1]++;
				else if ('2' <= ch && ch <= '9') poke[i][ch - '0']++;
				else if (ch == '1') { cin >> ch; poke[i][10]++; }
				else if (ch == 'J') poke[i][11]++;
				else if (ch == 'Q') poke[i][12]++;
				else if (ch == 'K') poke[i][13]++;
			}
		hand[1] = hand[2] = hand[3] = hand[4] = 13;
		int rndpoke = 0, rndplayer = 0;
		while (hand[1] && hand[2] && hand[3] && hand[4])
		{
			rndpoke = rndpoke % 13 + 1;
			rndplayer = rndplayer % 4 + 1;
			if (rndplayer == 1)
				if (poke[1][rndpoke])
				{
					poke[1][rndpoke]--; hand[1]--; table[rndpoke]++;
					int nextrndpoke = rndpoke % 13 + 1;
					if (poke[2][nextrndpoke] == 0)
						for (int i = 1; i <= 13; i++) { poke[2][i] += table[i]; hand[2] += table[i]; table[i] = 0; }
					else if (hand[1] == 0)
						for (int i = 1; i <= 13; i++) { poke[4][i] += table[i]; hand[4] += table[i]; table[i] = 0; }
				}
				else
				{
					int curpoke = -1;
					if (poke[1][10]) curpoke = 10;
					if (curpoke == -1) for (int i = 2; i <= 9; i++) if (poke[1][i]) { curpoke = i; break; }
					if (curpoke == -1)
						if (poke[1][1]) curpoke = 1;
						else if (poke[1][11]) curpoke = 11;
						else if (poke[1][13]) curpoke = 13;
						else curpoke = 12;
					poke[1][curpoke]--; hand[1]--; table[curpoke]++;
					int nextrndpoke = rndpoke % 13 + 1;
					if (poke[2][nextrndpoke] == 0 || poke[3][rndpoke] == 4 || hand[1] == 0)
						for (int i = 1; i <= 13; i++) { poke[1][i] += table[i]; hand[1] += table[i]; table[i] = 0; }
				}
			else if (rndplayer == 2)
				if (poke[2][rndpoke])
				{
					int num = poke[2][rndpoke]; 
					hand[2] -= num; poke[2][rndpoke] = 0;table[rndpoke] += num;
					if (hand[2] == 0)
						for (int i = 1; i <= 13; i++) { poke[4][i] += table[i]; hand[4] += table[i]; table[i] = 0; }
				}
				else
				{
					int curpoke = -1;
					if (poke[2][10]) curpoke = 10;
					if (curpoke == -1) for (int i = 2; i <= 9; i++) if (poke[2][i]) { curpoke = i; break; }
					if (curpoke == -1)
						if (poke[2][1]) curpoke = 1;
						else if (poke[2][11]) curpoke = 11;
						else if (poke[2][13]) curpoke = 13;
						else curpoke = 12;
					poke[2][curpoke]--;	hand[2]--; table[curpoke]++;
					if (poke[3][rndpoke] == 4 || hand[2] == 0 || 1 + poke[1][rndpoke] > 4)
						for (int i = 1; i <= 13; i++) {	poke[2][i] += table[i];	hand[2] += table[i]; table[i] = 0; }
				}
			else if (rndplayer == 3)
				if (poke[3][rndpoke])
				{
					int num = poke[3][rndpoke];
					hand[3] -= num;	poke[3][rndpoke] = 0; table[rndpoke] += num; 
					if (hand[3] == 0)
						for (int i = 1; i <= 13; i++) { poke[4][i] += table[i]; hand[4] += table[i]; table[i] = 0; }
				}
				else
				{
					int minnum = 4;
					for (int i = 1; i <= 13; i++)
						if (poke[3][i]) minnum = min(minnum, poke[3][i]);
					int curpoke = -1;
					if (poke[3][10] == minnum) curpoke = 10;
					if (curpoke == -1) for (int i = 2; i <= 9; i++)	if (poke[3][i] == minnum) {	curpoke = i; break;	}
					if (curpoke == -1)
						if (poke[3][1] == minnum) curpoke = 1;
						else if (poke[3][11] == minnum) curpoke = 11;
						else if (poke[3][13] == minnum) curpoke = 13;
						else curpoke = 12;
					hand[3] -= minnum; poke[3][curpoke] = 0; table[curpoke] += minnum;
					if (hand[3] == 0 || minnum + poke[1][rndpoke] > 4)
						for (int i = 1; i <= 13; i++) { poke[3][i] += table[i]; hand[3] += table[i]; table[i] = 0; }
				}
			else if (rndplayer == 4)
				if (poke[4][rndpoke] == 3 || poke[4][rndpoke] == 4)
				{
					int num = poke[4][rndpoke];
					hand[4] -= num; poke[4][rndpoke] = 0; table[rndpoke] += num;
					int nextrndpoke = rndpoke % 13 + 1;
					if (num + poke[1][rndpoke] > 4 || poke[1][nextrndpoke] == 0)
						for (int i = 1; i <= 13; i++) {	poke[1][i] += table[i];	hand[1] += table[i]; table[i] = 0; }
					else if (poke[3][rndpoke] == 4)
						for (int i = 1; i <= 13; i++) { poke[3][i] += table[i]; hand[3] += table[i]; table[i] = 0; }
				}
				else
				{
					int num1 = poke[4][rndpoke];
					hand[4] -= num1; poke[4][rndpoke] = 0; table[rndpoke] += num1;
					int num2 = 0;
					if (hand[4]) num2 = 1;
					int curpoke = -1;
					if (poke[4][10]) curpoke = 10;
					if (curpoke == -1)
						for (int i = 2; i <= 9; i++) if (poke[4][i]) { curpoke = i; break; }
					if (curpoke == -1)
						if (poke[4][1]) curpoke = 1;
						else if (poke[4][11]) curpoke = 11;
						else if (poke[4][13]) curpoke = 13;
						else curpoke = 12;
					if (num2) {	poke[4][curpoke] -= num2; table[curpoke] += num2; hand[4] -= num2; }
					int nextrndpoke = rndpoke % 13 + 1;
					if (num1 + num2 + poke[1][rndpoke] > 4 || poke[1][nextrndpoke] == 0)
						if (num2) for (int i = 1; i <= 13; i++) { poke[4][i] += table[i]; hand[4] += table[i]; table[i] = 0; }
						else for (int i = 1; i <= 13; i++) { poke[1][i] += table[i]; hand[1] += table[i]; table[i] = 0; }
					else if (poke[3][rndpoke] == 4)
						for (int i = 1; i <= 13; i++) { poke[4][i] += table[i]; hand[4] += table[i]; table[i] = 0; }
				}
		}
		for (int i = 1; i <= 4; i++)
			if (hand[i] == 0) cout << "WINNER" << endl;
			else for (int j = 1; j <= 13; j++)
				while (poke[i][j])
				{
					if (j == 1) cout << "A";
					else if (2 <= j && j <= 10) cout << j;
					else if (j == 11) cout << "J";
					else if (j == 12) cout << "Q";
					else if (j == 13) cout << "K";
					poke[i][j]--; hand[i]--;
					if (hand[i] == 0) cout << endl;
					else cout << " ";
				}
	}
	return 0;
}

D.80 Days【尺取】
题目链接:https://cn.vjudge.net/problem/HihoCoder-1831

#include 
#include 
using namespace std;
static const int MAXN = 1e6;
int a[MAXN * 2 + 5], b[MAXN * 2 + 5];
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n, c;
		cin >> n >> c;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			a[i + n] = a[i];
		}
		for (int i = 1; i <= n; i++)
		{
			cin >> b[i];
			b[i + n] = b[i];
		}
		long long sum = 1ll * c;
		queue<int> Q;
		while (!Q.empty())
			Q.pop();
		bool flag = true;
		for (int i = 1; i <= 2 * n; i++)
		{
			if (sum + a[i] - b[i] >= 0)
			{
				sum += 1ll*(a[i] - b[i]);
				Q.push(i);
				if (Q.size() >= n)
				{
					flag = false;
					cout << Q.front() << endl;
					Q.pop();
					break;
				}
			}
			else
			{
				while (sum + a[i] - b[i] < 0 && Q.size())
				{
					int pos = Q.front();
					Q.pop();
					sum -= 1ll*(a[pos] - b[pos]);
				}
				if (sum + a[i] - b[i] >= 0)
				{
					sum += 1ll*(a[i] - b[i]);
					Q.push(i);
					if (Q.size() >= n)
					{
						flag = false;
						cout << Q.front() << endl;
						break;
					}
				}
			}
		}
		if (flag)
			cout << -1 << endl;
	}
	return 0;
}

E.Odd Chess
题目链接:https://cn.vjudge.net/problem/HihoCoder-1832

F.Shortest Path Problem
题目链接:https://cn.vjudge.net/problem/HihoCoder-1833

G.The Mole
题目链接:https://cn.vjudge.net/problem/HihoCoder-1834

H.K-Dimensional Foil II
题目链接:https://cn.vjudge.net/problem/HihoCoder-1835

I.Order
题目链接:https://cn.vjudge.net/problem/HihoCoder-1836

J.Rikka with Polygon
题目链接:https://cn.vjudge.net/problem/HihoCoder-1837

你可能感兴趣的:(ICPC真题)