代码:
//POJ 1753
#include <stdio.h>
#include <string.h>
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int g[10][10];
int ans;
void change(int x, int y)
{
int i;
g[x][y] = !g[x][y];
for (i = 0; i < 4; i++)
g[x+dir[i][0]][y+dir[i][1]] = !g[x+dir[i][0]][y+dir[i][1]];
}
int work(int x, int y, int depth)
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
if (g[i][j] != g[1][1]) break;
if (j <= 4) break;
}
if (i > 4)
{
if (depth < ans) ans = depth;
return 1;
}
if (x > 4) return 1;
change(x,y);
if (y == 4) work(x+1, 1, depth+1);
else work(x, y+1, depth+1);
change(x,y);
if (y == 4) work(x+1, 1, depth);
else work(x, y+1, depth);
return 1;
}
int main()
{
int i, j;
char c;
ans = 100000000;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{
scanf("%c", &c);
g[i][j] = c == 'b' ? 1 : 0;
}
getchar();
}
work(1,1,0);
if (ans == 100000000) puts("Impossible");
else printf("%d\n", ans);
return 0;
}
//POJ 2965
#include <stdio.h>
#include <string.h>
int g[10][10];
int ans[10][10];
int pos[10][10];
int minnum = 100000000;
void change(int x,int y)
{
int i, j;
for (i = 1; i <= 4; i++) g[i][y] = !g[i][y];
for (i = 1; i <= 4; i++) g[x][i] = !g[x][i];
g[x][y] = !g[x][y];
}
int judge()
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
if (g[i][j]) break;
if (j <= 4) return 0;
}
return 1;
}
void copy()
{
int i, j;
for (i = 1; i <= 4; i++)
for (j = 1; j <= 4; j++)
ans[i][j] = pos[i][j];
}
void work(int x, int y, int depth)
{
if (judge())
{
if (depth < minnum) { minnum = depth; copy();}
return ;
}
if (x > 4) return ;
change(x, y);
pos[x][y] = 1;
if (y == 4) work(x+1, 1, depth+1);
else work(x, y+1, depth+1);
change(x, y);
pos[x][y] = 0;
if (y == 4) work(x+1, 1, depth);
else work(x, y+1, depth);
return ;
}
void outputAns()
{
int i, j;
printf("%d\n", minnum);
for (i = 1; i <= 4; i++)
for (j = 1; j <= 4; j++)
if (ans[i][j]) printf("%d %d\n", i, j);
}
void getInput()
{
int i, j;
char c;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 4; j++)
{
scanf("%c", &c);
g[i][j] = c == '+' ? 1 : 0;
}
getchar();
}
}
int main()
{
getInput();
work(1,1, 0);
outputAns();
return 0;
}
//POJ 1328
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct Node
{
double left, right;
}node[2100];
int cmp(Node a, Node b)
{
return a.left < b.left;
}
int main()
{
int i, j, n, d, x, y, ans, flag, ncas = 1;
double xx, p;
while (~scanf("%d%d", &n, &d))
{
if (n == 0 && d == 0) break;
flag = 0;
for (i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
if (y > d) flag = 1;
xx = sqrt(1.0*d*d-y*y*1.0);
node[i].left = x-xx;
node[i].right = x+xx;
}
if (flag) printf("Case %d: -1\n", ncas++);
else
{
ans = 0;
p = -10000000;
sort(node, node+n, cmp);
for (i = 0; i < n; i++)
{
if (node[i].left > p)
ans++, p = node[i].right;
else if (node[i].right < p) p = node[i].right;
}
printf("Case %d: %d\n", ncas++, ans);
}
}
return 0;
}
//POJ 3349 hash??????
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node
{
int f[6];
}Node;
Node snow[100010];
int input[6];
int flag = 0;
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int cmp1(const void *a, const void *b)
{
int i;
Node aa, bb;
aa = *(Node *)a, bb = *(Node *)b;
for (i = 0; i < 6; i++)
if (aa.f[i] != bb.f[i]) return aa.f[i] - bb.f[i];
flag = 1;
return 0;
}
int main()
{
int n, i, j, k, num, index = 0;
scanf("%d", &n);
while (n--)
{
for (i = 0; i < 6; i++)
scanf("%d", &input[i]);
qsort(input, 6, sizeof(int), cmp);
for (j = 0; j < 6; j++)
snow[index].f[j] = input[j];
index++;
}
qsort(snow, index, sizeof(Node), cmp1);
puts(flag ? "Twin snowflakes found." : "No two snowflakes are alike.");
return 0;
}#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node
{
int f[6];
}Node;
Node snow[100010];
int input[6];
int flag = 0;
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int cmp1(const void *a, const void *b)
{
int i;
Node aa, bb;
aa = *(Node *)a, bb = *(Node *)b;
for (i = 0; i < 6; i++)
if (aa.f[i] != bb.f[i]) return aa.f[i] - bb.f[i];
flag = 1;
return 0;
}
int main()
{
int n, i, j, k, num, index = 0;
scanf("%d", &n);
while (n--)
{
for (i = 0; i < 6; i++)
scanf("%d", &input[i]);
qsort(input, 6, sizeof(int), cmp);
for (j = 0; j < 6; j++)
snow[index].f[j] = input[j];
index++;
}
qsort(snow, index, sizeof(Node), cmp1);
puts(flag ? "Twin snowflakes found." : "No two snowflakes are alike.");
return 0;
}