1、Codeforces 11C How Many Squares?
解题思路:理解题意很重要...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 300;
int t, n, m, c1, c2, c3;
char graph[maxn][maxn];
bool vis[maxn][maxn];
int dir[][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
void dfs(int x, int y);
int check1(int x, int y);
int check2(int x, int y);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
memset(vis, false, sizeof(vis));
REP(i, n) { scanf("%s", graph[i]); }
int cnt = 0;
REP(i, n) REP(j, m) {
if (!vis[i][j] && graph[i][j] == '1') {
c1 = 0; c2 = 0; c3 = 0;
c1 = check1(i, j); c2 = check2(i, j); dfs(i, j);
if (c1 == c3 || c2 == c3) { ++cnt; }
}
}
printf("%d\n", cnt);
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
int check1(int x, int y) {
int row = 1, column = 1;
for (int i = x + 1; i < n && graph[i][y] == '1'; ++i) { ++row; }
for (int i = y + 1; i < m && graph[x][i] == '1'; ++i) { ++column; }
if (row < 2 || column < 2 || column != row) { return -1; }
for (int i = y + 1; i <= y + column - 1; ++i) {
if (graph[x + row - 1][i] != '1') { return -1; }
}
for (int i = x + 1; i < x + row - 1; ++i) {
if (graph[i][y + column - 1] != '1') { return -1; }
}
return 2 * (row + column - 2);
}
int check2(int x, int y) {
int t = 0;
for (int i = x + 1, j = y - 1, k = y + 1; i < n; ++i, --j, ++k) {
if (j >= 0 && k < m && graph[i][j] == '1' && graph[i][k] == '1') {
++t;
} else {
break;
}
}
if (t < 1) { return -1; }
for (int i = x + t + 1, j = y - t + 1, k = y + t - 1; i < x + 2 * t; ++i, ++j, --k) {
if (!(i < n && graph[i][j] == '1' && graph[i][k] == '1')) {
return -1;
}
}
if (!(x + 2 * t < n && graph[x + 2 * t][y] == '1')) { return -1; }
return 4 * t;
}
void dfs(int x, int y) {
vis[x][y] = true; ++c3;
for (int i = 0; i < 8; ++i) {
int x_t = x + dir[i][0], y_t = y + dir[i][1];
if (0 <= x_t && x_t < n && 0 <= y_t && y_t < m && !vis[x_t][y_t] && graph[x_t][y_t] == '1') {
dfs(x_t, y_t);
}
}
}
2、UVa 656 Optimal Programs
虽然是搜索题,但是感觉写成了模拟题,调了一上午,网站也不配合,一到我就不给测了。。。QAQ
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 20;
struct Node {
int num[maxn], cmd[maxn];
int num_len, cmd_len;
};
int x[maxn], y[maxn];
int n;
vector<Node> ans;
char key[][10] = {"ADD", "DIV", "DUP", "MUL", "SUB"};
void bfs(void);
bool judge(Node _node, int _x, int _y);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
int Case = 0;
while (scanf("%d", &n) && n != 0) {
REP(i, n) { scanf("%d", &x[i]); }
REP(i, n) { scanf("%d", &y[i]); }
ans.clear();
printf("Program %d\n", ++Case);
bool all_same = true;
REP(i, n) {
if (x[i] != y[i]) { all_same = false; break; }
}
if (all_same) { printf("Empty sequence\n\n"); continue; }
bfs();
int Size = ans.size();
bool have_ans = false;
REP(i, Size) {
bool flag = true;
for (int j = 1; j < n; ++j) {
if (!judge(ans[i], x[j], y[j])) { flag = false; break; }
}
if (flag) {
REP(j, ans[i].cmd_len - 1) { printf("%s ", key[ans[i].cmd[j] - 1]); }
printf("%s\n\n", key[ans[i].cmd[ans[i].cmd_len - 1] - 1]);
have_ans = true; break;
}
}
if (!have_ans) { printf("Impossible\n\n"); }
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
bool judge(Node _node, int _x, int _y) {
stack<int> s; s.push(_x);
REP(i, _node.cmd_len) {
if (_node.cmd[i] == 3) { int t = s.top(); s.push(t); continue; }
if (s.size() < 2) { return false; }
int t_1 = s.top(); s.pop();
int t_2 = s.top(); s.pop();
if (_node.cmd[i] == 1) {
int t = t_1 + t_2;
if (abs(t) > 30000) { return false; }
s.push(t);
} else if (_node.cmd[i] == 5) {
int t = t_2 - t_1;
if (abs(t) > 30000) { return false; }
s.push(t);
} else if (_node.cmd[i] == 4) {
int t = t_1 * t_2;
if (abs(t) > 30000) { return false; }
s.push(t);
} else {
if (t_1 == 0) { return false; }
s.push(t_2 / t_1);
}
}
if (s.size() == 1 && s.top() == _y) { return true; }
return false;
}
void bfs(void) {
queue<Node> q;
Node node; node.num[0] = x[0]; node.num_len = 1; node.cmd_len = 0;
q.push(node);
while (!q.empty()) {
Node t = q.front(); q.pop();
if (t.num_len == 1 && t.num[0] == y[0]) { ans.push_back(t); }
if (t.cmd_len >= 10) { continue; }
if (t.num_len > 1) {
if (abs(t.num[0] + t.num[1]) <= 30000) {
node.num_len = t.num_len - 1; node.cmd_len = t.cmd_len + 1;
node.num[0] = t.num[0] + t.num[1]; node.cmd[t.cmd_len] = 1;
REP(i, t.cmd_len) { node.cmd[i] = t.cmd[i]; }
for (int i = 1; i < node.num_len; ++i) { node.num[i] = t.num[i + 1]; }
q.push(node);
}
if (t.num[0] != 0) {
node.num_len = t.num_len - 1; node.cmd_len = t.cmd_len + 1;
node.num[0] = t.num[1] / t.num[0]; node.cmd[t.cmd_len] = 2;
REP(i, t.cmd_len) { node.cmd[i] = t.cmd[i]; }
for (int i = 1; i < node.num_len; ++i) { node.num[i] = t.num[i + 1]; }
q.push(node);
}
}
node.num_len = t.num_len + 1; node.cmd_len = t.cmd_len + 1;
node.num[0] = t.num[0]; node.cmd[t.cmd_len] = 3;
REP(i, t.cmd_len) { node.cmd[i] = t.cmd[i]; }
for (int i = 1; i < node.num_len; ++i) { node.num[i] = t.num[i - 1]; }
q.push(node);
if (t.num_len > 1) {
if (abs(t.num[0] * t.num[1]) <= 30000) {
node.num_len = t.num_len - 1; node.cmd_len = t.cmd_len + 1;
node.num[0] = t.num[0] * t.num[1]; node.cmd[t.cmd_len] = 4;
REP(i, t.cmd_len) { node.cmd[i] = t.cmd[i]; }
for (int i = 1; i < node.num_len; ++i) { node.num[i] = t.num[i + 1]; }
q.push(node);
}
if (abs(t.num[1] - t.num[0]) <= 30000) {
node.num_len = t.num_len - 1; node.cmd_len = t.cmd_len + 1;
node.num[0] = t.num[1] - t.num[0]; node.cmd[t.cmd_len] = 5;
REP(i, t.cmd_len) { node.cmd[i] = t.cmd[i]; }
for (int i = 1; i < node.num_len; ++i) { node.num[i] = t.num[i + 1]; }
q.push(node);
}
}
}
}
3、ZOJ 3950 How Many Nines
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x7fffffff;
int T, Y1, m1, d1, y2, m2, d2, ans;
int key[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int num[10000], sum[10000];
bool is_run(int y) {
if (y % 400 == 0) { return true; }
if (y % 100 == 0) { return false; }
return (y % 4) == 0;
}
int sum_y(int y) {
int ret = 0; while (y) { if ((y % 10) == 9) { ++ret; } y /= 10; } return ret;
}
void init(void) {
for (int i = 2000; i <= 9999; ++i) {
if (!is_run(i)) { num[i] = 65 + sum_y(i) * 365; }
else { num[i] = 66 + sum_y(i) * 366; }
sum[i] = sum[i - 1] + num[i];
}
}
int sum_d(int y, int m_1, int d_1, int m_2, int d_2) {
int ret = 0; bool flag = is_run(y);
if (m_1 == m_2) { ret += d_2 - d_1 + 1; return ret; }
if (m_1 == 2 && flag) { ret += 29 - d_1 + 1; } else { ret += key[m_1 - 1] - d_1 + 1; }
for (int i = m_1 + 1; i <= m_2 - 1; ++i) {
if (i == 2 && flag) { ret += 29; } else { ret += key[i - 1]; }
}
ret += d_2;
return ret;
}
int sum_m(int y, int m_1, int d_1, int m_2, int d_2) {
int ret = 0;
if (m_1 == m_2) {
if (m_1 == 9) { ret += (d_2 - d_1 + 1); }
for (int i = d_1; i <= d_2; ++i) { if ((i % 10) == 9) { ++ret; } }
return ret;
}
for (int i = m_1 + 1; i <= m_2 - 1; ++i) {
ret += 3; if (i == 2 && !is_run(y)) { --ret; } if (i == 9) { ret += 30; }
}
if (m_1 == 9) { ret += 30 - d_1 + 1; }
if (m_2 == 9) { ret += d_2; }
if (d_1 <= 9) { ret += 3; } else if (d_1 <= 19) { ret += 2; } else if (d_1 <= 29) { ++ret; }
if (m_1 == 2 && !is_run(y)) { --ret; }
if (d_2 >= 29) { ret += 3; } else if (d_2 >= 19) { ret += 2; } else if (d_2 >= 9) { ++ret; }
return ret;
}
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
scanf("%d", &T);
init();
while (T--) {
scanf("%d %d %d %d %d %d", &Y1, &m1, &d1, &y2, &m2, &d2);
ans = 0;
if (y2 - Y1 - 1 > 0) { ans += sum[y2 - 1] - sum[Y1]; }
if (Y1 == y2) {
ans += sum_m(Y1, m1, d1, m2, d2); ans += sum_y(Y1) * sum_d(Y1, m1, d1, m2, d2);
} else {
ans += sum_m(Y1, m1, d1, 12, 31); ans += sum_y(Y1) * sum_d(Y1, m1, d1, 12, 31);
ans += sum_m(y2, 1, 1, m2, d2); ans += sum_y(y2) * sum_d(y2, 1, 1, m2, d2);
}
printf("%d\n", ans);
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}