谷歌中國演算法比賽解題報告 APAC2015D
阿新 • • 發佈:2019-01-28
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 <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 class PA { public: PA(){} int S; vector<vector<int>> 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 <S-1 && matrix[row][col+1] - 1 == matrix[row][col]) return DFS(row, col+1) + 1; return 1; } void SingleProcess(ofstream& fout) { int maxi, maxj; int maxPath = -1; for (int i = 0; i < S; i++) { for (int j = 0; j < S; j++) { int temp = DFS(i, j); if (temp>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<int>(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<int> buses; vector<int> 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<string, string> tickets; map<string, string> reverse_tickets; map<string, string>::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<pair<int, int>> dirs; int repeatTimes; //在一個方向上重複幾次 MoveType(){ repeatTimes = 1; } }; MoveType K, Q, R, B, N, P; vector<vector<char>> board; void preDefine() { K.dirs.push_back(pair<int, int>(0, -1)); K.dirs.push_back(pair<int, int>(0, 1)); K.dirs.push_back(pair<int, int>(1, -1)); K.dirs.push_back(pair<int, int>(1, 0)); K.dirs.push_back(pair<int, int>(1, 1)); K.dirs.push_back(pair<int, int>(-1, -1)); K.dirs.push_back(pair<int, int>(-1, 0)); K.dirs.push_back(pair<int, int>(-1, 1)); K.repeatTimes = 1; Q.dirs.push_back(pair<int, int>(0, -1)); Q.dirs.push_back(pair<int, int>(0, 1)); Q.dirs.push_back(pair<int, int>(1, -1)); Q.dirs.push_back(pair<int, int>(1, 0)); Q.dirs.push_back(pair<int, int>(1, 1)); Q.dirs.push_back(pair<int, int>(-1, -1)); Q.dirs.push_back(pair<int, int>(-1, 0)); Q.dirs.push_back(pair<int, int>(-1, 1)); Q.repeatTimes = INT_MAX; R.dirs.push_back(pair<int, int>(0, -1)); R.dirs.push_back(pair<int, int>(0, 1)); R.dirs.push_back(pair<int, int>(1, 0)); R.dirs.push_back(pair<int, int>(-1, 0)); R.repeatTimes = INT_MAX; B.dirs.push_back(pair<int, int>(1, -1)); B.dirs.push_back(pair<int, int>(1, 1)); B.dirs.push_back(pair<int, int>(-1, -1)); B.dirs.push_back(pair<int, int>(-1, 1)); B.repeatTimes = INT_MAX; N.dirs.push_back(pair<int, int>(1, -2)); N.dirs.push_back(pair<int, int>(1, 2)); N.dirs.push_back(pair<int, int>(2, -1)); N.dirs.push_back(pair<int, int>(2, 1)); N.dirs.push_back(pair<int, int>(-1, -2)); N.dirs.push_back(pair<int, int>(-1, 2)); N.dirs.push_back(pair<int, int>(-2, -1)); N.dirs.push_back(pair<int, int>(-2, 1)); N.repeatTimes = 1; P.dirs.push_back(pair<int, int>(1, -1)); P.dirs.push_back(pair<int, int>(-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<int, int> pi = mt.dirs[i]; int nr = row; int nc = col; int count = 0; while (count<mt.repeatTimes) { nr += pi.first; nc += pi.second; if (nr<0 || nr>7 || 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<char>(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; }