//错误在于漏掉了初始状态的检查#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#define N 13#define M 1594323 // pow(3, 13)typedefstruct node {
int a[N];
int d;
} Pos;
int n;
Pos *p[M], *begin;
Pos *queue[M+1];
int front, rear;
void initQueue()
{
front = rear = 0;
}
int isEmpty()
{
return front == rear;
}
void push(Pos *p1)
{
queue[rear++] = p1;
}
Pos *pop()
{
returnqueue[front++];
}
void init()
{
initQueue();
memset(p, 0, sizeof(p));
}
int hash(int a[])
{
int i, res = 0;
for (i=0; i<n; i++)
res = res*3 + a[i];
return res;
}
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
int checkCode(int a[])
{
int i, j;
int code[4] = {2, 0, 1, 2};
for (i=0; i<n-3; i++)
{
j = i;
while (j-i < 4 && a[j] == code[j-i])
j++;
if (j-i == 4)
return1;
}
return0;
}
int BFS(Pos *begin)
{
int i, j, add;
begin->d = 0;
if (checkCode(begin->a))
return begin->d;
push(begin);
while (!isEmpty())
{
Pos *p1 = pop();
for (i=0; i<n-1; i++)
{
swap(&(p1->a[i]), &(p1->a[i+1]));
add = hash(p1->a);
if (p[add] == NULL)
{
p[add] = (Pos *)malloc(sizeof(Pos));
for (j=0; j<n; j++)
p[add]->a[j] = p1->a[j];
p[add]->d = p1->d + 1;
if (checkCode(p[add]->a))
return p[add]->d;
push(p[add]);
}
swap(&(p1->a[i]), &(p1->a[i+1]));
}
}
return -1;
}
int main()
{
int i, add, a[N];
char s[N+1];
while(scanf("%d%s", &n, s) != EOF)
{
for (i=0; i<n; i++)
a[i] = s[i] - '0';
init();
add = hash(a);
p[add] = (Pos *)malloc(sizeof(Pos));
for (i=0; i<n; i++)
p[add]->a[i] = a[i];
begin = p[add];
printf("%d\n", BFS(begin));
}
return0;
}
/**************************************************************
Problem: 1482
User: liangrx06
Language: C
Result: Accepted
Time:200 ms
Memory:26092 kb
****************************************************************/
1483
#include <stdio.h>#define N 10000
int main(void)
{
int n, i; int a; while (scanf("%d", &n) != EOF) { int max, min; for(i=0; i<n; i++) { scanf("%d", &a); if (i == 0) { max = min = a; continue; } if (a < min) min = a; if (a > max) max = a; } printf("%d %d\n", max, min); } return 0;
}
/**************************************************************
Problem: 1483 User: liangrx06 Language: C Result: Accepted Time:40 ms Memory:912 kb****************************************************************/
1484
#include <stdio.h>
#include <string.h>
int main()
{
int miles;
char s1[200], s2[200], type[2];
int sum = 0;
while(scanf("%s", s1) != EOF)
{
if (strcmp(s1, "0") == 0)
{
printf("%d\n", sum);
sum = 0;
continue;
}
elseif (strcmp(s1, "#") == 0)
break;
scanf("%s%d%s", s2, &miles, type);
if (type[0] == 'Y')
sum += ((miles <= 500) ? 500 : miles);
else
{
sum += 2*miles;
if (type[0] == 'B')
sum -= (miles/2);
}
}
return0;
}
/**************************************************************
Problem: 1484
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/
1485
#include <stdio.h>#include <string.h>#include <stdlib.h>#define N 80typedefstruct node {
int i;
char c;
} Letter;
int cmp(constvoid *a, constvoid *b)
{
return ((Letter *)a)->i - ((Letter *)b)->i;
}
int main()
{
char s[N+1];
Letter L[N], *p[3][N];
int len[3];
int k[3];
int n, i, j;
while(scanf("%d%d%d", &k[0], &k[1], &k[2]) != EOF)
{
if (!k[0] && !k[1] && !k[2])
break;
scanf("%s", s);
n = strlen(s);
len[0] = len[1] = len[2] = 0;
for (i=0; i<n; i++)
{
L[i].i = i;
L[i].c = s[i];
if (s[i] >= 'a' && s[i] <= 'i')
p[0][len[0]++] = &L[i];
elseif (s[i] >= 'j' && s[i] <= 'r')
p[1][len[1]++] = &L[i];
else
p[2][len[2]++] = &L[i];
}
for (i=0; i<3; i++)
{
int tmp[N];
for (j=0; j<len[i]; j++)
tmp[j] = p[i][j]->i;
for (j=0; j<len[i]; j++)
p[i][j]->i = tmp[(j+k[i])%len[i]];
}
qsort(L, n, sizeof(L[0]), cmp);
for (i=0; i<n; i++)
printf("%c", L[i].c);
printf("\n");
}
return0;
}
/**************************************************************
Problem: 1485
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/
1486(未完成)
1487(未完成)
1488
#include <stdio.h>
int main(void)
{
printf("%d %d\n", 10*30, (1<<30)-1); return 0;
}
/**************************************************************
Problem: 1488 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:908 kb****************************************************************/
1481(未完成)1482//错误在于漏掉了初始状态的检查#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#define N 13#define M 1594323 // pow(3, 13)typedef struct node { int a[N]; int d;}