谷歌中国算法比赛解题报告 APAC2015D

Problem A. Cube IV

Problem B. GBus count

Problem C. Sort a scrambled itinerary

Problem D. Itz Chess


作为2015年的最后一场APAC,谷歌你真的打算在这场比赛里招人么?????

这四道题都毫无难度可言,而且最后一题,对从没玩过国际象棋的我……你的描述不清晰,棋盘要扭着头看也就算了,小数据的testcase一跑就过,大数据却错了,找来找去,最后百度了一下国际象棋的走法,我才知道喵了个咪的题理解错了


最后,附上简略攻略。。。

1. 对每个点DFS,找出最长的就好了

2. 木桶原理扫一遍

3. 把每一对节点关系都存一下,然后随便找一个节点,不停的找爹,找到不能找位置,这个节点就是起始节点,然后从这个节点一直往后走到尾就好了

4.把每个棋子的走法定义一下,一个是方向,一个是在这个方向上能重复几次,然后每个棋子算一遍就好了


最后,附上代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define ll long long


class PA
{
public:
	PA(){}
	int S;
	vector> matrix;

	int DFS(int row, int col)
	{
		if (row > 0 && matrix[row - 1][col] - 1 == matrix[row][col]) return DFS(row - 1, col) + 1;
		if (row < S-1 && matrix[row + 1][col] - 1 == matrix[row][col]) return DFS(row + 1, col) + 1;
		if (col > 0 && matrix[row][col-1] - 1 == matrix[row][col]) return DFS(row, col-1) + 1;
		if (col maxPath || (maxPath == temp&&matrix[i][j] < matrix[maxi][maxj]))
				{
					maxPath = temp;
					maxi = i;
					maxj = j;
				}
			}
		}

		fout << matrix[maxi][maxj] << ' ' << maxPath;
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d", &S);
			matrix.clear();
			matrix.resize(S, vector(S, 0));
			for (int i = 0; i < S; i++)
			{
				for (int j = 0; j < S; j++)
				{
					scanf("%d", &matrix[i][j]);
				}
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PB
{
public:
	PB(){}
	int N, P;
	vector buses;
	vector cities;

	int SingleProcess(ofstream& fout)
	{
		for (int i = 0; i < P; i++)
		{
			int check = cities[i];
			int count = 0;
			for (int j = 0; j < buses.size(); j += 2)
			{
				if (buses[j] <= check&&buses[j + 1] >= check) count++;
			}
			fout << count << " ";
		}
		return 0;
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d", &N);
			buses.resize(2*N,0);
			for (int i = 0; i < N; i++)
			{
				scanf("%d %d", &buses[i*2], &buses[i*2 + 1]);
			}

			scanf("%d", &P);
			cities.resize(P, 0);
			for (int i = 0; i < P; i++)
			{
				scanf("%d", &cities[i]);
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PC
{
public:
	PC(){}
	int N;
	map tickets;
	map reverse_tickets;
	map::iterator iter;

	int SingleProcess(ofstream& fout)
	{
		string head = reverse_tickets.begin()->second;
		iter = reverse_tickets.find(head);
		while (iter != reverse_tickets.end())
		{
			head = iter->second;
			iter = reverse_tickets.find(head);
		}
		
		iter = tickets.find(head);
		while (iter != tickets.end())
		{
			fout << iter->first.c_str() << "-" << iter->second.c_str() << " ";
			iter = tickets.find(iter->second);
		}

		return 0;
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d", &N);
			tickets.clear();
			reverse_tickets.clear();
			for (int i = 0; i < N; i++)
			{
				char ch1[1024];
				char ch2[1024];
				scanf("%s", ch1);
				scanf("%s", ch2);
				tickets[ch1] = ch2;
				reverse_tickets[ch2] = ch1;
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};

class PD
{
public:
	PD(){}
	int Pieces;
	struct MoveType
	{
		vector> dirs;
		int repeatTimes; //在一个方向上重复几次
		MoveType(){ repeatTimes = 1; }
	};

	MoveType K, Q, R, B, N, P;
	vector> board;
	void preDefine()
	{
		K.dirs.push_back(pair(0, -1));
		K.dirs.push_back(pair(0, 1));
		K.dirs.push_back(pair(1, -1));
		K.dirs.push_back(pair(1, 0));
		K.dirs.push_back(pair(1, 1));
		K.dirs.push_back(pair(-1, -1));
		K.dirs.push_back(pair(-1, 0));
		K.dirs.push_back(pair(-1, 1));
		K.repeatTimes = 1;

		Q.dirs.push_back(pair(0, -1));
		Q.dirs.push_back(pair(0, 1));
		Q.dirs.push_back(pair(1, -1));
		Q.dirs.push_back(pair(1, 0));
		Q.dirs.push_back(pair(1, 1));
		Q.dirs.push_back(pair(-1, -1));
		Q.dirs.push_back(pair(-1, 0));
		Q.dirs.push_back(pair(-1, 1));
		Q.repeatTimes = INT_MAX;

		R.dirs.push_back(pair(0, -1));
		R.dirs.push_back(pair(0, 1));
		R.dirs.push_back(pair(1, 0));
		R.dirs.push_back(pair(-1, 0));
		R.repeatTimes = INT_MAX;

		B.dirs.push_back(pair(1, -1));
		B.dirs.push_back(pair(1, 1));
		B.dirs.push_back(pair(-1, -1));
		B.dirs.push_back(pair(-1, 1));
		B.repeatTimes = INT_MAX;

		N.dirs.push_back(pair(1, -2));
		N.dirs.push_back(pair(1, 2));
		N.dirs.push_back(pair(2, -1));
		N.dirs.push_back(pair(2, 1));
		N.dirs.push_back(pair(-1, -2));
		N.dirs.push_back(pair(-1, 2));
		N.dirs.push_back(pair(-2, -1));
		N.dirs.push_back(pair(-2, 1));
		N.repeatTimes = 1;

		P.dirs.push_back(pair(1, -1));
		P.dirs.push_back(pair(-1, -1));
		P.repeatTimes = 1;
	}


	int getKillNumber(int row, int col)
	{
		char c = board[row][col];
		MoveType mt;
		if (c == 'P') mt = P;
		else if (c == 'K') mt = K;
		else if (c == 'Q') mt = Q;
		else if (c == 'R') mt = R;
		else if (c == 'B') mt = B;
		else if (c == 'N') mt = N;

		int kill = 0;
		for (int i = 0; i < mt.dirs.size(); i++)
		{
			pair pi = mt.dirs[i];
			int nr = row;
			int nc = col;
			int count = 0;
			while (count7 || nc<0 || nc>7) break;
				if (board[nr][nc] != 0)
				{
					kill++;
					break;
				}
				count++;
			}
		}
		return kill;

	}


	void SingleProcess(ofstream& fout)
	{
		int count = 0;
		for (int i = 0; i < 8; i++)
		{
			for (int j = 0; j < 8; j++)
			{
				if (board[i][j] != 0)
				{
					count += getKillNumber(i,j);
				}
			}
		}
		fout << count;
	}

	void run()
	{
		FILE* fp = freopen("in.txt", "r", stdin);
		ofstream fout("out.txt");
		int Cases = 0;
		scanf("%d", &Cases);
		preDefine();
		for (int time = 0; time < Cases; time++)
		{
			scanf("%d", &Pieces);
			board.clear();
			board.resize(8, vector(8, 0));
			for (int i = 0; i < Pieces; i++)
			{
				char ch[1024];
				scanf("%s", ch);
				board[ch[1] - '1'][7 - (ch[0] - 'A')] = ch[3];
			}

			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};





int main()
{
	//PA p;
	//PB p;
	//PC p;
	PD p;
	p.run();

	return 0;
}


你可能感兴趣的:(APAC)