@lawk97
2017-06-18T03:36:46.000000Z
字数 12217
阅读 1051
枚举
贪心
构造法
模拟法
[poj1753,poj2965]
A - Flip Game
[POJ - 1753]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
struct temp{
int x,y,step;
char gra[4][4];
}t;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
temp convert(temp t){
for(int i = 0; i < 4; i++){
int x = t.x + dir[i][0], y = t.y + dir[i][1];
if(x >= 0 && x < 4 && y >= 0 && y < 4){
t.gra[x][y] = (t.gra[x][y] == 'b') ? 'w':'b';
}
}
t.gra[t.x][t.y] = (t.gra[t.x][t.y] == 'b') ? 'w':'b';
t.step++;
return t;
}
temp move(temp t){
if(t.x < 3){
t.x++;
}else{
t.x=0;
t.y++;
}
return t;
}
bool ensure(temp t){
char c=t.gra[0][0];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(t.gra[i][j]!=c){
return false;
}
}
}
return true;
}
int main(){
int ans=1e9;
for(int i = 0; i < 4; i++){
scanf("%s",t.gra[i]);
}
t.x=0;
t.y=0;
t.step=0;
stack<temp> s;
s.push(t);
t=convert(t);
s.push(t);
while(!s.empty()){
temp top = s.top();
s.pop();
if (ensure(top)){
ans = (ans > top.step) ? top.step:ans;
continue;
}
top = move(top);
if (top.y >= 4)
{
continue;
}
s.push(top);
top = convert(top);
s.push(top);
}
if (ans==1e9)
{
printf("Impossible\n");
}else{
printf("%d\n",ans);
}
}
B - The Pilots Brothers' refrigerator
[POJ - 2965]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
//两种枚举思路,口感很好的递归练习
int ans,ansx[20],ansy[20],gra;
int pathx[20],pathy[20];
char g[4][4];
void convert(int x,int y){
gra = gra ^ (1 << (x+y*4));
for(int i = 0; i < 4; i++){
gra = gra ^ (1 << (i+y*4));
}
for(int i = 0; i < 4; i++){
gra = gra ^ (1 << (x+i*4));
}
}
bool ensure(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if( ( (gra>>(j+i*4) ) & 1) != 1){
return false;
}
}
}
return true;
}
void dfs(int step,int x,int y){
if (ensure())
{
if (ans > step){
ans = step;
for (int i = 0; i < ans; ++i)
{
pathx[i]=ansx[i];
pathy[i]=ansy[i];
}
}
return;
}
if (y >= 4 || step > 16)
{
return;
}
if (x < 3)
{
dfs(step,x+1,y);
}else
{
dfs(step,0,y+1);
}
convert(x,y);
ansx[step]=x;
ansy[step]=y;
if (x < 3)
{
dfs(step+1,x+1,y);
}else
{
dfs(step+1,0,y+1);
}
convert(x,y);
}
int main(){
for(int i = 0; i < 4; i++){
scanf("%s",g[i]);
}
gra=0;
ans=1e9;
int num,o=1;
for (int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; j++){
num = (g[i][j]=='-') ? 1:0;
gra+=num*o;
o = o<<1;
}
}
dfs(0,0,0);
printf("%d\n",ans);
for (int i = 0; i < ans; ++i)
{
printf("%d %d\n",pathy[i]+1,pathx[i]+1 );
}
}
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
//要求先枚举翻转的情况
int ans,gra,flag;
int pathx[20],pathy[20];
char g[4][4];
void convert(int x,int y){
gra = gra ^ (1 << (x+y*4));
for(int i = 0; i < 4; i++){
gra = gra ^ (1 << (i+y*4));
}
for(int i = 0; i < 4; i++){
gra = gra ^ (1 << (x+i*4));
}
}
bool ensure(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if( ( (gra>>(j+i*4) ) & 1) != 1){
return false;
}
}
}
return true;
}
void dfs(int step,int x,int y){
if (ans == step)
{
flag=ensure();
return ;
}
if(flag || (y==4)){
return ;
}
convert(x,y);
pathx[step]=x;
pathy[step]=y;
if (x < 3)
{
dfs(step+1,x+1,y);
}else
{
dfs(step+1,0,y+1);
}
convert(x,y);
if (x < 3)
{
dfs(step,x+1,y);
}else
{
dfs(step,0,y+1);
}
}
int main(){
for(int i = 0; i < 4; i++){
scanf("%s",g[i]);
}
gra=0;
flag=0;
int num,o=1;
for (int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; j++){
num = (g[i][j]=='-') ? 1:0;
gra+=num*o;
o = o<<1;
}
}
for (ans = 1; ans <= 16; ++ans)
{
dfs(0,0,0);
if (flag)
{
break;
}
}
if (ans==1e9)
{
printf("Impossible\n");
}else{
printf("%d\n",ans);
for (int i = 0; i < ans; ++i)
{
printf("%d %d\n",pathy[i]+1,pathx[i]+1 );
}
}
}
[poj1328,poj2109,poj2586]
C - Radar Installation
[POJ - 1328]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
struct location{
int x,y;
};
struct path
{
double l,r;
};
bool cmp(path a, path b){
return a.l<b.l;
}
int main(){
int n,d,kase=0;
location l[1005];
path p[1005];
while(scanf("%d%d",&n,&d),n!=0){
for(int i = 0; i < n; i++){
scanf("%d%d",&l[i].x,&l[i].y);
}
int ans=0,flag=0;
for(int i = 0; i < n; i++){
if (d<l[i].y)
{
flag=1;
break;
}
p[i].r=l[i].x+sqrt(d*d-l[i].y*l[i].y);
p[i].l=l[i].x-sqrt(d*d-l[i].y*l[i].y);
}
printf("Case %d: ",++kase );
if (flag){
printf("-1\n");
}else{
sort(p,p+n,cmp);
for(int i = 0; i < n; i++){
double x = p[i].r;
ans++;
for(int j = i+1; j < n; j++){
if(x < p[j].l){
break;
}else if(x > p[j].r){
x = p[j].r;
}
i = j;
}
}
printf("%d\n",ans );
}
}
return 0;
}
D - Power of Cryptography
[POJ - 2109]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
/*
基础知识: 类型 长度 (bit) 有效数字 绝对值范围
float 32 6~7 10^(-37) ~ 10^38
double 64 15~16 10^(-307) ~ 10^308
long double 128 18~19 10^(-4931) ~ 10 ^ 4932
double scanf use %lf,printf use %f
long double scanf&printf use %Lf,but some compiler don't suppose it
*/
int main(){
double n,p;
while(~scanf("%lf%lf",&n,&p)){
cout << pow(p,1/n) << endl;
}
return 0;
}
E - Y2K Accounting Bug
[POJ - 2586]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
int main(){
long long d,s,all,num,m[15];
while(~scanf("%lld%lld",&d,&s)){
all=0;
m[0]=0;
for(int i = 1; i <= 4; i++){
all += d;
m[i] = d;
if( all-(5-i)*s > 0 ){
all -= d;
all += -s;
m[i] = -s;
}
}
for(int i=5;i<=12;i++){
all -= m[i-5];
if (all+d<0){
all += d;
m[i] = d;
}else
{
all += -s;
m[i] = -s;
}
}
all=0;
for(int i = 1; i <= 12; i++){
all+=m[i];
}
if (all>0)
{
printf("%lld\n",all );
}else{
printf("Deficit\n");
}
}
return 0;
}
[poj3295]
F - Tautology
[POJ - 3295]
题意
5个0-1变量(p,q,r,s,t),5种简单操作(K,A,N,C,E),给出表达式,判断该表达式的结果是否恒为1。
五种操作原理如下:
Kab = a&&b;
Aab = a||b;
Na = !a;
Cab = (!a)||b;
Eab = (a==b).
思路
易发现变量都是在操作符后面的,所以可以用一个栈管理变量,从表达式最右侧开始,遇变量则压栈,遇操作符出栈变量并将结果压栈。最后栈里将仅存一个变量,即为表达式的解。
5个变量,取值共有2^5种,依次判别,看有无零解即可。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
char text[105];
stack<int> va;
map<char,int> m;
bool cal(int p,int q,int r,int s,int t){
m['p']=p;
m['q']=q;
m['r']=r;
m['s']=s;
m['t']=t;
int len=strlen(text);
for(int i = len-1;i >= 0; i--){
if (text[i]=='p'||text[i]=='q'
||text[i]=='r'||text[i]=='s'
||text[i]=='t')
{
va.push(m[text[i]]);
continue;
}else if (text[i]=='N')
{
int t = !(va.top());
va.pop();
va.push(t);
continue;
}
int a,b;
a = va.top();
va.pop();
b = va.top();
va.pop();
if (text[i]=='K')
{
va.push(a&&b);
}else if (text[i]=='A')
{
va.push(a||b);
}else if (text[i]=='C')
{
va.push( (!a) || b );
}else if (text[i]=='E')
{
va.push(a==b);
}
}
return va.top();
}
int main(){
while(scanf("%s",text),text[0]!='0'){
int flag=1;
for(int p = 0; flag && p<=1; p++){
for(int q = 0; flag && q<=1; q++){
for(int r = 0; flag && r<=1; r++){
for(int s = 0; flag && s<=1; s++){
for(int t = 0; flag && t<=1; t++){
flag=cal(p,q,r,s,t);
}
}
}
}
}
if (flag)
{
printf("tautology\n");
}else{
printf("not\n");
}
}
return 0;
}
[poj1068,poj2632,poj1573,poj2993,poj2996)]
G - Parencodings
[POJ - 1068]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
int main(){
int t,n,p[25],w[25];
char sym[100000];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d",&p[i]);
}
int num = 0;
stack<int> s;
for(int i = 0; i < n; i++){
for(int j = 0; j < p[i]-num; j++){
sym[i+num+j] = '(';
}
sym[i+p[i]] = ')';
num = p[i];
}
int len = strlen(sym);
num = 0;
for(int i = 0; i < len; i++){
if(sym[i]=='('){
s.push(i);
num++;
}else if(s.empty()){
w[i-num] = 0;
}else{
int top = s.top();
s.pop();
w[i-num] = (i-top+1)/2;
}
}
for (int i = 0; i < n; ++i)
{
printf("%d ",w[i]);
}
printf("\n");
}
return 0;
}
H - Crashing Robots
[POJ - 2632]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
struct robot{
int x,y,d;
}r[105];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int main(){
int t,n,m,a,b;
int gra[105][105];
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&a,&b,&n,&m);
for(int i = 0; i < 105; i++){
for(int j = 0; j < 105; j++){
gra[i][j] = 0;
}
}
for(int i = 1; i <= n; i++){
char dir;
scanf("%d%d",&r[i].x,&r[i].y);
getchar();
dir=getchar();
if(dir == 'N'){
r[i].d = 0;
}else if(dir == 'E'){
r[i].d = 1;
}else if(dir == 'S'){
r[i].d = 2;
}else if(dir == 'W'){
r[i].d = 3;
}
gra[r[i].x][r[i].y] = i;
}
int flag = 0;
for(int i = 0; i < m; i++){
int now,num;
char ins;
scanf("%d",&now);
getchar();
ins = getchar();
scanf("%d",&num);
if (flag)
{
continue;
}
if(ins=='L'){
r[now].d = (r[now].d+3*num)%4;
}else if(ins=='R'){
r[now].d = (r[now].d+num)%4;
}else{
while(num--){
gra[r[now].x][r[now].y] = 0;
r[now].x += dir[r[now].d][0];
r[now].y += dir[r[now].d][1];
if(r[now].x<=0 || r[now].x>a
|| r[now].y<=0 || r[now].y>b){
flag = 1;
printf("Robot %d crashes into the wall\n",now );
break;
}else if (gra[r[now].x][r[now].y])
{
flag = 1;
printf("Robot %d crashes into robot %d\n",
now,gra[r[now].x][r[now].y] );
break;
}
gra[r[now].x][r[now].y] = now;
}
}
}
if (!flag)
{
printf("OK\n");
}
}
return 0;
}
I - Robot Motion
[POJ - 1573]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
struct location{
int x,y;
};
int main(){
int r,c,e,used[12][12];
char gra[12][12];
while(scanf("%d%d%d",&r,&c,&e),r&&c){
for(int i = 0; i < r; i++){
scanf("%s",gra[i]);
}
for(int i = 0; i < 12; i++){
for(int j = 0; j < 12; j++){
used[i][j] = -1;
}
}
int ansg = 0, ansl = 0;
stack<location> s;
location st;
st.x = e-1;
st.y = 0;
used[st.y][st.x] = 0;
s.push(st);
while(!s.empty()){
location top = s.top();
s.pop();
int x = top.x;
int y = top.y;
switch(gra[y][x]){
case 'N':
y--;
break;
case 'E':
x++;
break;
case 'S':
y++;
break;
case 'W':
x--;
break;
default:
break;
}
ansg++;
if(x<0 || x>=c || y<0 || y>=r){
printf("%d step(s) to exit\n",ansg );
break;
}else if(used[y][x] >= 0){
printf("%d step(s) before a loop of %d step(s)\n"
,used[y][x], ansg-used[y][x] );
break;
}
used[y][x] = ansg;
top.y = y;
top.x = x;
s.push(top);
}
}
return 0;
}
J - Emag eht htiw Em Pleh
[POJ - 2993]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
int main(){
char gra[20][40];
for(int i = 0; i < 17; i += 2){
for(int j = 0; j < 33; j++){
if (j % 4 == 0)
{
gra[i][j] = '+';
}else{
gra[i][j] = '-';
}
}
}
for(int i = 1; i < 16; i += 2){
for(int j = 0; j < 33; j++){
if (j % 4 == 0)
{
gra[i][j] = '|';
}else if( ((j/4)+(i/2)) % 2 == 0){
gra[i][j] = '.';
}else{
gra[i][j] = ':';
}
}
}
char text[100];
scanf("%s",text);
scanf("%s",text);
int len = strlen(text);
for(int i = 0; i < len; i++){
int ch,x,y;
if (text[i]>='A'&&text[i]<='Z')
{
ch = text[i];
x = 15 - (text[i+2]-'1')*2;
y = (text[i+1]-'a')*4 + 2;
i += 3;
}else{
ch = 'P';
x = 15 - (text[i+1]-'1')*2;
y = (text[i]-'a')*4 + 2;
i += 2;
}
gra[x][y] = ch;
}
scanf("%s",text);
scanf("%s",text);
len = strlen(text);
for(int i = 0; i < len; i++){
int ch,x,y;
if (text[i]>='A'&&text[i]<='Z')
{
ch = text[i];
x = 15 - (text[i+2]-'1')*2;
y = (text[i+1]-'a')*4 + 2;
i += 3;
}else{
ch = 'P';
x = 15 - (text[i+1]-'1')*2;
y = (text[i]-'a')*4 + 2;
i += 2;
}
gra[x][y] = ch + 32;
}
for(int i = 0; i < 17; i++){
for(int j = 0; j < 33; j++){
printf("%c",gra[i][j] );
}
printf("\n");
}
}
K - Help Me with the Game
[POJ - 2996]
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
using namespace std;
struct chees{
int x[10];
char y[10];
};
map<char,chees> mloc;
map<char,int> mused;
char sheet[12]
= {'K','Q','R','B','N','P','k','q','r','b','n','p'};
char gra[20][40];
int main(){
for(int i = 0; i < 17; i++){
scanf("%s",gra[i]);
}
for(int i = 0; i < 12; i++){
mused[sheet[i]] = 0;
}
for(int i = 15; i >= 0; i -= 2){
for(int j = 2; j < 33; j += 4){
int c = gra[i][j];
if(mused[c]>=10){
continue;
}
mloc[c].x[ mused[c] ] = 8-i/2;
mloc[c].y[ mused[c] ] = 'a'+(j-2)/4;
mused[c]++;
}
}
printf("White: ");
for(int i = 0; i < 6; i++){
int c = sheet[i];
for(int j = 0; j < mused[c]; j++){
if (i < 5)
{
printf("%c", c);
}
printf("%c%d", mloc[c].y[j], mloc[c].x[j]);
if(i==5&&j==(mused[c]-1)){
continue;
}
printf(",");
}
}
printf("\n");
for(int i = 0; i < 12; i++){
mused[sheet[i]] = 0;
}
for(int i = 1; i <= 15; i += 2){
for(int j = 2; j < 33; j += 4){
int c = gra[i][j];
if(mused[c]>=10){
continue;
}
mloc[c].x[ mused[c] ] = 8-i/2;
mloc[c].y[ mused[c] ] = 'a'+(j-2)/4;
mused[c]++;
}
}
printf("Black: ");
for(int i = 6; i < 12; i++){
int c = sheet[i];
for(int j = 0; j < mused[c]; j++){
if (i < 11)
{
printf("%c", c-32);
}
printf("%c%d", mloc[c].y[j], mloc[c].x[j]);
if(i==11&&j==(mused[c]-1)){
continue;
}
printf(",");
}
}
printf("\n");
return 0;
}