PROBLEM A The Fault in Our Cubes
题意:
给出三种形状的木块,即他们的顺序,问是否能按用绳子按序穿出一个 3 * 3 的立方体。
思路:
根据顺序暴力搜索加回溯即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 28;
const int xx[6] = {1, 0, 0, -1, 0, 0};
const int yy[6] = {0, 0, 1, 0, 0, -1};
const int zz[6] = {0, -1, 0, 0, 1, 0};
char str[maxn];
int g[5][5][5];
bool flag;
void dfs(int x, int y, int z, int dir, int pos) {
if (g[x][y][z]) return;
g[x][y][z] = 1;
if (x < 1 || x > 3 || y < 1 || y > 3 || z < 1 || z > 3)
return;
if (pos == 26) {
flag = 1;
return;
}
if (str[pos] == 'E') {
dfs(x+1, y, z, dir, pos+1);
}
if (str[pos] == 'I') {
dfs(x+xx[dir], y+yy[dir], z+zz[dir], dir, pos+1);
}
if (str[pos] == 'L') {
for(int i = 0; i < 6 ; i++)
if (i != dir && i != (dir+3)%6) {
dfs(x+xx[i], y+yy[i], z+zz[i], i, pos+1);
}
}
g[x][y][z] = 0;
return;
}
bool solve() {
for(int i = 1; i < 4; i++)
for(int j = 1; j < 4; j++)
for(int k = 1; k < 4; k++)
dfs(i, j, k, 0, 0);
return flag;
}
int main() {
scanf("%s", str);
if (solve())
cout<<"YES";
else
cout<<"NO";
cout<<endl;
return 0;
}
PROBLEM C Arcade
题意:在一个R*C的矩阵中,有3种字符,你从g[1][1]开始只能向下或向右移动,到了哪个格子就可以收集那个格子的字符,最后要使收集到字符尽量平均,输出收集的最少的字符的数量;
思路:
首先用深度搜索,想了一些剪枝但是最终没达到质变,无奈超时;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 108;
int res = 0, c, r;
char g[MAXN][MAXN];
void dfs(int x, int y, int aa, int bb, int cc) {
int k = (res-aa)>0 ? res-aa:0 + (res-bb)>0 ? res-bb:0 + (res-cc)>0 ? res-cc:0;
//cout<<x<<' '<<y<<' '<<res<<endl;
if (k > r+c-1-aa-bb-cc) return ;
if (x == r && y == c) {
res = min(aa, bb);
res = min(res, cc);
return ;
}
for(int i = 0; i < 2; i++)
if (x+i <= r && y+1-i <= c) {
int xx = x+i;
int yy = y+1-i;
//cout<<i<<' '<<xx<<' '<<yy<<' '<<r<<' '<<c<<endl;
if (g[xx][yy] == '>')
dfs(xx, yy, aa+1, bb, cc);
if (g[xx][yy] == '-')
dfs(xx, yy, aa, bb+1, cc);
if (g[xx][yy] == '|')
dfs(xx, yy, aa, bb, cc+1);
}
return ;
}
int main() {
char cc;
scanf("%d%d", &r, &c);
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++) {
cin>>cc;
g[i][j] = cc;
}
/*
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++)
printf("%c", g[i][j]);
puts(" ");
}
cout<< g[1][1]<<endl;
*/
if (g[1][1] == '>')
dfs(1, 1, 1, 0, 0);
if (g[1][1] == '-')
dfs(1, 1, 0, 1, 0);
if (g[1][1] == '|')
dfs(1, 1, 0, 0, 1);
cout<<res<<endl;
return 0;
}
然后直接暴力加一些优化;终于A了;
代码:
写的有些麻烦
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 208;
int t[MAXN][MAXN][MAXN];
char g[108][108];
struct numstack {
int num[2][108][20108][3];
int l[108][108];
} wj;
int change(char cc) {
if (cc == '>')
return 0;
if (cc == '-')
return 1;
if (cc == '|')
return 2;
return -1;
}
int main() {
int r, c, i, j;
scanf("%d%d", &r, &c);
int v = 0;
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
cin>>g[i][j];
int tmp = change(g[i][j]);
//cout<<tmp<<endl;
if (i == 1 && j == 1) {
wj.num[v^1][1][0][tmp] = 1;
wj.l[i][j] = 1;
}
if (j-1 > 0)
for(int k = 0; k < wj.l[i][j-1]; k++) {
for(int p = 0; p < 3; p++)
wj.num[v^1][j][wj.l[i][j]][p] = wj.num[v^1][j-1][k][p];
wj.num[v^1][j][wj.l[i][j]][tmp]++;
//cout<<v<<' '<<j<<' '<<wj.l[i][j]<<' '<<tmp<<endl;
if (!t[wj.num[v^1][j][wj.l[i][j]][0]][wj.num[v^1][j][wj.l[i][j]][1]][wj.num[v^1][j][wj.l[i][j]][2]]) {
t[wj.num[v^1][j][wj.l[i][j]][0]][wj.num[v^1][j][wj.l[i][j]][1]][wj.num[v^1][j][wj.l[i][j]][2]] = 1;
wj.l[i][j]++;
}
}
if (i-1 > 0)
for(int k = 0; k < wj.l[i-1][j]; k++) {
for(int p = 0; p < 3; p++)
wj.num[v^1][j][wj.l[i][j]][p] = wj.num[v][j][k][p];
wj.num[v^1][j][wj.l[i][j]][tmp]++;
if (!t[wj.num[v^1][j][wj.l[i][j]][0]][wj.num[v^1][j][wj.l[i][j]][1]][wj.num[v^1][j][wj.l[i][j]][2]]) {
t[wj.num[v^1][j][wj.l[i][j]][0]][wj.num[v^1][j][wj.l[i][j]][1]][wj.num[v^1][j][wj.l[i][j]][2]] = 1;
wj.l[i][j]++;
}
}
for(int k = 0; k < wj.l[i][j]; k++)
t[wj.num[v^1][j][k][0]][wj.num[v^1][j][k][1]][wj.num[v^1][j][k][2]] = 0;
}
v ^= 1;
}
int res = 0;
for(i = 0; i <= wj.l[r][c]; i++) {
j = min(wj.num[v][c][i][0], wj.num[v][c][i][1]);
j = min(j, wj.num[v][c][i][2]);
res = max(res, j);
}
cout<<res<<endl;
return 0;
}
总结:最好别嵌套用结构体,不然一个简单的数要写好长;哎呀,还是随便啦!
PROBLEM D - !Hasan
题意:
一共有n台电脑需要调试,甲调试一台需要x分钟,乙调试一台需要y分钟,问调试完全部电脑最少需要多少分钟
思路:
一:二分答案
二:推公式
设甲修了 A 台电脑,乙修了 B 台电脑
由题意有:
(1)A + B = N
(2)ANS = MAX (Ax , By)
故只有两种情况
若 Ax >= By
即 A >= n * Y / ( X + Y )
若 Ax < By
即 B > n * X / ( X + Y )
两种情况取最小
代码:
二分:
我是2分的电脑数量:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int maxn = 100008;
int main() {
__int64 result, n, x, y, tmp, timex, timey, l, r;
scanf("%I64d%I64d%I64d", &n, &x, &y);
if (x > y) swap(x, y);
l = 1;
r = n;
tmp = n/2;
timex = tmp*x;
timey = (n-tmp)*y;
while (abs(timey - timex) > y) {
if (timey > timex) {
l = tmp+1;
}
else {
r = tmp-1;
}
tmp = (l+r)/2;
timex = tmp*x;
timey = (n-tmp)*y;
}
result = timex;
if (timex < timey)
result = timey;
if (timex + x < timey)
result = timex + x;
printf("%I64d\n", result);
return 0;
}
推公式:(注意精度)
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
__int64 x, y, n, ans, sum, x1, y1;
cin>>n>>x>>y;
sum=x+y;
x1=n*y/sum;
if((n*y)%sum)
x1++;
y1=n*x/sum;
if((n*x)%sum)
y1++;
ans=min(y1*y,x1*x);
cout<<ans<<endl;
return 0;
}