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

A.Read Phone Number

B.Rational Number Tree

C.Sorting

D.Cross the maze

E.Spaceship Defence


解题分析:

A.这道题应该算练手题,没什么难度


B.观察分析这道题,可以发现所有分子比分母大的都在右枝,分子比分母小的都在左枝,所以如果知道一个数字,那么倒着推过去,就知道它在每一层的左右枝情况,就能求出它是第几个数字,同理,知道第几个数字,那么发现第一层有1个,第二层2个,第三层4个数字。。。,先判断出这个数字在哪一层,是第几个,然后就回到了倒着推得情况,记录路径,就能得出这个数字的p,q


C.这一题,先扫一遍数组,把奇数和奇数对应的位置都记录下来,偶数和偶数对应的位置记录下来,然后排序一遍奇数,再按记录的位置一个个写进去,偶数同理。


D.因为机器人开始在四个角落,所以左手必然靠着墙,先初始化好机器人的方向,然后如果左手没有墙,就往左手方向走,左手有墙,就沿着当前方向走,走不动就按顺时针旋转,直到找到能走的方向为止。


E.注意题目上颜色相同的房间能够直接传送,所以它们可以被看做同一个节点,这样原题目本来有很多很多节点,把颜色相同的节点当做一个后,就只剩不到1000个了,之后运用floyd算法,五行搞定任意两个房间的最短距离。


最后附上程序

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <hash_map>
#include <hash_set>
#include <unordered_map>
#include <unordered_set>
#include <string.h>
#include <queue>
#include <list>
#include <iomanip>

using namespace std;

#define ll long long
#define uint unsigned int
#define ull unsigned long long

class PA
{
public:
	PA(){}
	int M, N;

	vector<string> xxple;
	vector<string> numWord;

	string number;
	vector<int> segment;

	void predefine()
	{
		xxple.resize(11, "");
		xxple[2] = "double";
		xxple[3] = "triple";
		xxple[4] = "quadruple";
		xxple[5] = "quintuple";
		xxple[6] = "sextuple";
		xxple[7] = "septuple";
		xxple[8] = "octuple";
		xxple[9] = "nonuple";
		xxple[10] = "decuple";

		numWord.resize(10, "");
		numWord[0] = "zero";
		numWord[1] = "one";
		numWord[2] = "two";
		numWord[3] = "three";
		numWord[4] = "four";
		numWord[5] = "five";
		numWord[6] = "six";
		numWord[7] = "seven";
		numWord[8] = "eight";
		numWord[9] = "nine";
	}

	void foutNumber(ofstream& fout,int count,char currNum)
	{
		if (currNum != 0)
		{
			if (count>1 && count < 11)
			{
				fout << xxple[count].c_str() << " " << numWord[currNum - '0'].c_str() << " ";
			}
			else
			{
				for (int k = 0; k < count; k++)
				{
					fout << numWord[currNum - '0'].c_str() << " ";
				}
			}
		}
	}

	void SingleProcess(ofstream& fout)
	{
		int totalCount = 0;
		for (int i = 0; i < segment.size(); i++)
		{
			int count = 0;
			char currNum = 0;
			for (int j = 0; j < segment[i]; j++,totalCount++)
			{
				if (currNum != number[totalCount])
				{
					foutNumber(fout, count, currNum);
					currNum = number[totalCount];
					count = 1;
				}
				else
				{
					count++;
				}
			}
			foutNumber(fout, count, currNum);
		}
	}

	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++)
		{
			char ch[1024];
			scanf("%s", ch);
			number = ch;
			scanf("%s", ch);
			segment.clear();
			string temp = ch;
			string::size_type st = temp.find_first_of('-');
			while (st!=temp.npos)
			{
				segment.push_back(atoi(temp.substr(0, st).c_str()));
				temp = temp.substr(st + 1);
				st = temp.find_first_of('-');
			}
			segment.push_back(atoi(temp.c_str()));

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

class PB
{
public:
	PB(){}
	ull ID, P, Q, N;

	void SingleProcess(ofstream& fout)
	{
		ull level = 0;
		ull index = 0;
		if (ID == 1)
		{
			ull t1 = 1;
			ull t2 = N;
			while (t1 < t2)
			{
				t2 -= t1;
				t1 = t1 << 1;
				level++;
			}
			index = t2;

			ull p, q;
			p = q = 1;
			ull l1 = 1;
			for (int i = 1; i <= level; i++)
			{
				ull mu = l1 << (level - i);
				if (mu >= index)
				{
					q = p + q;
				}
				else
				{
					index -= mu;
					p = p + q;
				}
			}
			fout << p << " " << q;
		}
		else if (ID == 2)
		{
			ull p, q;
			p = P; q = Q;
			ull mu = 1;
			level = 1;
			while (p != 1 || q != 1)
			{
				if (p > q)
				{
					index += mu;
					p = p - q;
				}
				else
				{
					q = q - p;
				}
				level++;
				mu = mu << 1;
			}

			ull temp = 1;
			temp = (temp << (level - 1));
			fout << (temp + index);
		}
	}

	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++)
		{
			cin >> ID;
			if (ID == 1)
			{
				cin >> N;
			}
			else if (ID == 2)
			{
				cin >> P >> Q;
			}

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

class PC
{
public:
	PC(){}
	int N;
	vector<int> books;
	

	void SingleProcess(ofstream& fout)
	{
		vector<int> alex;
		vector<int> bob;
		vector<bool> whosOnPosition(N,false);
		for (int i = 0; i < N; i++)
		{
			if (abs(books[i]) % 2 == 1)
			{
				whosOnPosition[i] = false;
				alex.push_back(books[i]);
			}
			else
			{
				whosOnPosition[i] = true;
				bob.push_back(books[i]);
			}
		}

		sort(alex.begin(), alex.end());
		sort(bob.begin(), bob.end());
		reverse(bob.begin(), bob.end());

		int count = 0;
		for (int i = 0; i < books.size(); i++)
		{
			if (whosOnPosition[i] == false)
			{
				books[i] = alex[count];
				count++;
			}
		}

		count = 0;
		for (int i = 0; i < books.size(); i++)
		{
			if (whosOnPosition[i] == true)
			{
				books[i] = bob[count];
				count++;
			}
		}

		for (int i = 0; i < books.size(); i++)
		{
			fout << books[i] << " ";
		}

	}

	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++)
		{
			cin >> N;
			books.resize(N,0);
			for (int i = 0; i < books.size(); i++)
			{
				cin >> books[i];
			}


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

class PD
{
public:
	PD(){}
	struct Dir
	{
		int row;
		int col;
		Dir(){ row = col = 0; }
	};
	int N;
	vector<vector<char>> maze;
	int SX, SY, EX, EY;

	bool adjustFacing(Dir& facing, Dir& leftHand, Dir& currPos)
	{
		int row = currPos.row + leftHand.row;
		int col = currPos.col + leftHand.col;
		if (row >= 0 && row < N&&col >= 0 && col < N && maze[row][col]=='.')
		{
			facing = leftHand;

			leftHand.row = -facing.col;
			leftHand.col = facing.row;
			return true;
		}

		for (int i = 0; i < 4; i++)
		{
			row = currPos.row + facing.row;
			col = currPos.col + facing.col;
			if (row >= 0 && row < N&&col >= 0 && col < N && maze[row][col] == '.')
			{
				leftHand.row = -facing.col;
				leftHand.col = facing.row;
				return true;
			}
			else
			{
				leftHand = facing;

				facing.row = leftHand.col;
				facing.col = -leftHand.row;
			}
		}

		return false;

	}

	char getDirFromFacing(Dir& facing)
	{
		if (facing.row == 1) return 'S';
		if (facing.row == -1) return 'N';
		if (facing.col == 1) return 'E';
		if (facing.col == -1) return 'W';
		return '?';
	}


	void SingleProcess(ofstream& fout)
	{
		string path;
		Dir facing, leftHand,currPos;

		SX--; SY--; EX--; EY--;

		if (SX == 0) leftHand.row = -1;
		else if (SX == N - 1) leftHand.row = 1;

		facing.row = leftHand.col;
		facing.col = -leftHand.row;
		currPos.row = SX;
		currPos.col = SY;


		for (int i = 0; i < 10000; i++)
		{
			if(adjustFacing(facing, leftHand, currPos))
			{
				path.push_back(getDirFromFacing(facing));
				currPos.row = currPos.row + facing.row;
				currPos.col = currPos.col + facing.col;
				if (currPos.row == EX&&currPos.col == EY)
				{
					fout << i + 1 << endl << path.c_str();
					return;
				}
			}
			else
			{
				break;
			}
		}

		fout << "Edison ran out of energy.";
	}

	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++)
		{
			cin >> N;
			maze.clear();
			maze.resize(N, vector<char>(N, 0));
			for (int i = 0; i < N; i++)
			{
				for (int j = 0; j < N; j++)
				{
					cin >> maze[i][j];
				}
			}
			cin >> SX >> SY >> EX >> EY;
			fout << "Case #" << (time + 1) << ": ";
			SingleProcess(fout);
			fout << endl;
			std::cout << time << endl;
		}
		fclose(fp);
		fout.close();
	}
};


class PE
{
public:
	PE(){}
	int M, N, S;

	vector<string> rooms;
	vector<pair<int, int>> soldiers;
	vector<vector<int>> turbos;

	void SingleProcess(ofstream& fout)
	{
		map<string, int> idMap;
		map<string, int>::iterator iter;
		int index = 0;
		for (int i = 0; i < rooms.size(); i++)
		{
			iter = idMap.find(rooms[i]);
			if (iter == idMap.end())
			{
				idMap[rooms[i]] = index;
				index++;
			}
		}

		vector<vector<int>> matrix(index, vector<int>(index, 1e9+7));
		for (int i = 0; i < index; i++)
		{
			matrix[i][i] = 0;
		}
		for (int i = 0; i < turbos.size(); i++)
		{
			int id1 = idMap[rooms[turbos[i][0] - 1]];
			int id2 = idMap[rooms[turbos[i][1] - 1]];
			matrix[id1][id2] = min(matrix[id1][id2], turbos[i][2]);
		}

		for (int k = 0; k < index; k++)
		{
			for (int i = 0; i < index; i++)
			{
				for (int j = 0; j < index; j++)
				{
					matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]);
				}
			}
		}

		for (int i = 0; i < soldiers.size(); i++)
		{
			int from = idMap[rooms[soldiers[i].first - 1]];
			int to = idMap[rooms[soldiers[i].second - 1]];
			if (matrix[from][to] == 1e9 + 7)
			{
				fout << "\n" << -1;
			}
			else
			{
				fout << "\n" << matrix[from][to];
			}
		}

	}

	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++)
		{
			cin >> N;
			rooms.resize(N);
			for (int i = 0; i < N; i++)
			{
				char ch[7];
				cin >> ch;
				rooms[i] = ch;
			}

			cin >> M;
			turbos.resize(M, vector<int>(3, 0));
			for (int i = 0; i < M; i++)
			{
				cin >> turbos[i][0] >> turbos[i][1] >> turbos[i][2];
			}

			cin >> S;
			soldiers.resize(S,make_pair(0,0));
			for (int i = 0; i < S; i++)
			{
				cin >> soldiers[i].first >> soldiers[i].second;
			}

			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;
	PE p;
	p.run();

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值