您的位置:首页 > 编程语言 > Go语言

Google 2015 校招第四轮在线技术笔试 解题报告

2014-11-09 17:28 525 查看
题目在这里:https://code.google.com/codejam/contest/6214486/dashboard

真是各种水啊,满分无鸭梨。。。一个半小时就全部搞定了。

A. 记忆化搜索,或者叫备忘录式的动态规划也行,复杂度O(N * N)

#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#define FOR(i, n) for (int i = 0; i < n; ++i)
using namespace std;

const int MAX_N = 1005;
int dp[MAX_N][MAX_N], arr[MAX_N][MAX_N];
const int dir_x[] = {0, 0, -1, 1};
const int dir_y[] = {1, -1, 0, 0};
int S;
int solve(int x, int y) {
if (dp[x][y] > 0) return dp[x][y];
dp[x][y] = 1;
FOR(k, 4) {
int xx = x + dir_x[k], yy = y + dir_y[k];
if (xx < 0 || yy < 0 || xx >= S || yy >= S) continue;
if (arr[x][y] + 1 != arr[xx][yy]) continue;
dp[x][y] = max(dp[x][y], solve(xx, yy) + 1);
}
return dp[x][y];
}
int main() {
int T;
cin >> T;
for (int tt = 1; tt <= T; ++tt) {
cin >> S;
FOR(i, S) FOR(j, S) { cin >> arr[i][j]; dp[i][j] = 0; }
int mx = -1, pos = -1;
FOR(i, S) FOR(j, S) {
solve(i, j);
if (dp[i][j] > mx) { mx = dp[i][j]; pos = arr[i][j]; }
else if (dp[i][j] == mx && arr[i][j] < pos) pos = arr[i][j];
}
cout << "Case #" << tt << ": " << pos << " " << mx << endl;
}
return 0;
}


B. 大水题,暴力枚举就可以了

#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#define FOR(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main() {
int T, N, res, city, P;
cin >> T;
for (int tt = 1; tt <= T; ++tt) {
cin >> N;
vector<pair<int, int> > v;
int x, y;
FOR(i, N) { cin >> x >> y; v.push_back(make_pair(x, y)); }
cin >> P;
cout << "Case #" << tt << ":";
FOR(i, P) {
cin >> city;
res = 0;
FOR(j, v.size()) res += (city >= v[j].first && city <= v[j].second);
cout << " " << res;
}
cout << endl;
}
return 0;
}


C. 每条航班相当于有向图中的一个点,找到入度为零的那个点作为起始点,向下访问就可以啦

#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#define FOR(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int main() {
int T, N;
cin >> T;
for (int tt = 1; tt <= T; ++tt) {
cin >> N;
map<string, string> g;
map<string, int> cnt;
FOR(i, N) {
string from, to;
cin >> from >> to;
g[from] = to;
if (cnt.find(from) == cnt.end()) cnt[from] = 0;
if (cnt.find(to) == cnt.end()) cnt[to] = 1;
else ++cnt[to];
}
string s = "";
for (map<string, int>::iterator it = cnt.begin(); it != cnt.end(); ++it) {
if ((it->second) == 0) { s = it->first; break; }
}
assert(s != "");
cout << "Case #" << tt << ":";
while (g.find(s) != g.end()) { cout << " " << s << "-" << g[s]; s = g[s]; }
cout << endl;
}
return 0;
}


D.没什么难度,就是模拟起来比较恶心。。

#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#define FOR(i, n) for (int i = 0; i < n; ++i)
using namespace std;

const int dir_x[] = {0, 0, -1, 1, 1, 1, -1, -1};
const int dir_y[] = {-1, 1, 0, 0, -1, 1, -1, 1};

const int nx[] = {2, 2, 1, -1, -2, -2, 1, -1};
const int ny[] = {1, -1, 2, 2, 1, -1, -2, -2};
bool check(int x, int y) {
if (x < 0 || y < 0 || x >= 8 || y >= 8) return false;
return true;
}
int main() {
int T, N;
char g[8][8];
string s;
cin >> T;
for (int tt = 1; tt <= T; ++tt) {
cout << "Case #" << tt << ": ";
cin >> N;
FOR(i, 8) FOR(j, 8) g[i][j] = '.';
FOR(i, N) {
cin >> s;
int r = s[0] - 'A', c = s[1] - '1';
g[r][c] = s[3];
}
int res = 0;
FOR(i, 8) { FOR(j, 8) {
// cout << "here" << endl;
if (g[i][j] == '.') continue;
if (g[i][j] == 'K') {
FOR(k, 8) {
int xx = i + dir_x[k], yy = j + dir_y[k];
if (!check(xx, yy)) continue;
if (g[xx][yy] != '.') ++res;
}
}
if (g[i][j] == 'Q' || g[i][j] == 'R' || g[i][j] == 'B' || g[i][j] == 'P') {
int begin = 0, end = 8, cnt = 9999;
if (g[i][j] == 'R') end = 4;
else if (g[i][j] == 'B') begin = 4;
else if (g[i][j] == 'P') { begin = 4; end = 6; cnt = 1; }
for (int k = begin; k < end; ++k) {
int x = i, y = j;
int tmp = cnt;
while (cnt--) {
int xx = x + dir_x[k], yy = y + dir_y[k];
x = xx; y = yy;
// cout << "debug: " << xx << " " << yy << endl;
if (!check(xx, yy)) break;
if (g[xx][yy] != '.') { ++res; break; }
}
cnt = tmp;
}
}
if (g[i][j] == 'N') {
FOR(k, 8) {
int xx = i + nx[k], yy = j + ny[k];
if (!check(xx, yy)) continue;
if (g[xx][yy] != '.') { ++res; }
}
}
} }
cout << res << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐