最基础的广搜题。没有做任何优化,测试数据生成了超过一万五千个结点,效率一般。
Accepted | 1103 | C++ | 00:00.02 | 628K |
代码相当长,懒的压缩了。。。
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s
{
int p1,p2,p3;
struct s* next;
}node;
char g[50][50 ];
bool judge[50][50][50 ];
void setjudge(int p1,int p2,int p3)
{
judge[p1][p2][p3] = true ;
judge[p1][p3][p2] = true ;
judge[p2][p1][p3] = true ;
judge[p2][p3][p1] = true ;
judge[p3][p1][p2] = true ;
judge[p3][p2][p1] = true ;
}
node* newnode(int p1,int p2,int p3)
{
node* nd = (node*)malloc(sizeof (node));
nd->p1 = p1;
nd->p2 = p2;
nd->p3 = p3;
nd->next = NULL;
setjudge(p1,p2,p3);
return nd;
}
void freeall(node* head)
{
node* next;
while (head)
{
next = head-> next;
free(head);
head = next;
}
}
int bfs(node* head,int n)
{
if(head->p1 == head->p2 && head->p2 == head-> p3)
return 0 ;
node* next;
node* step = head;
node* tail = head;
int cnt = 1 ;
do
{
int p1 = head-> p1;
int p2 = head-> p2;
int p3 = head-> p3;
char color = g[p1][p2];
for(int i = 0; i < n; i++ )
{
if(i != p3 && color == g[p3][i] && ! judge[p1][p2][i])
{
if(p1 == p2 && p2 == i)
{
freeall(head);
return cnt;
}
tail->next = newnode(p1,p2,i);
tail = tail-> next;
}
}
color = g[p1][p3];
for(int i = 0; i < n; i++ )
{
if(i != p2 && color == g[p2][i] && ! judge[p1][i][p3])
{
if(p1 == i && i == p3)
{
freeall(head);
return cnt;
}
tail->next = newnode(p1,i,p3);
tail = tail-> next;
}
}
color = g[p2][p3];
for(int i = 0; i < n; i++ )
{
if(i != p1 && color == g[p1][i] && ! judge[i][p2][p3])
{
if(i == p2 && p2 == p3)
{
freeall(head);
return cnt;
}
tail->next = newnode(i,p2,p3);
tail = tail-> next;
}
}
if(head == step)
{
step = tail;
cnt++ ;
}
next = head-> next;
free(head);
head = next;
}while(head != NULL);
return -1 ;
}
void solve(int n)
{
memset(judge,0,sizeof (judge));
int p1,p2,p3;
scanf("%d%d%d/n",&p1,&p2,& p3);
node* head = newnode(--p1,--p2,-- p3);
for(int i = 0; i < n; i++ )
for(int j = 0; j < n; j++ )
scanf("%c%*[/n ]",& g[i][j]);
int res = bfs(head,n);
res == -1 ? printf("impossible/n") : printf("%d/n" ,res);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1103.txt","r" ,stdin);
freopen("ans.txt","w" ,stdout);
#endif
int n;
while(scanf("%d",&n) != EOF && n != 0 )
solve(n);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
}
#include <stdlib.h>
#include <string.h>
typedef struct s
{
int p1,p2,p3;
struct s* next;
}node;
char g[50][50 ];
bool judge[50][50][50 ];
void setjudge(int p1,int p2,int p3)
{
judge[p1][p2][p3] = true ;
judge[p1][p3][p2] = true ;
judge[p2][p1][p3] = true ;
judge[p2][p3][p1] = true ;
judge[p3][p1][p2] = true ;
judge[p3][p2][p1] = true ;
}
node* newnode(int p1,int p2,int p3)
{
node* nd = (node*)malloc(sizeof (node));
nd->p1 = p1;
nd->p2 = p2;
nd->p3 = p3;
nd->next = NULL;
setjudge(p1,p2,p3);
return nd;
}
void freeall(node* head)
{
node* next;
while (head)
{
next = head-> next;
free(head);
head = next;
}
}
int bfs(node* head,int n)
{
if(head->p1 == head->p2 && head->p2 == head-> p3)
return 0 ;
node* next;
node* step = head;
node* tail = head;
int cnt = 1 ;
do
{
int p1 = head-> p1;
int p2 = head-> p2;
int p3 = head-> p3;
char color = g[p1][p2];
for(int i = 0; i < n; i++ )
{
if(i != p3 && color == g[p3][i] && ! judge[p1][p2][i])
{
if(p1 == p2 && p2 == i)
{
freeall(head);
return cnt;
}
tail->next = newnode(p1,p2,i);
tail = tail-> next;
}
}
color = g[p1][p3];
for(int i = 0; i < n; i++ )
{
if(i != p2 && color == g[p2][i] && ! judge[p1][i][p3])
{
if(p1 == i && i == p3)
{
freeall(head);
return cnt;
}
tail->next = newnode(p1,i,p3);
tail = tail-> next;
}
}
color = g[p2][p3];
for(int i = 0; i < n; i++ )
{
if(i != p1 && color == g[p1][i] && ! judge[i][p2][p3])
{
if(i == p2 && p2 == p3)
{
freeall(head);
return cnt;
}
tail->next = newnode(i,p2,p3);
tail = tail-> next;
}
}
if(head == step)
{
step = tail;
cnt++ ;
}
next = head-> next;
free(head);
head = next;
}while(head != NULL);
return -1 ;
}
void solve(int n)
{
memset(judge,0,sizeof (judge));
int p1,p2,p3;
scanf("%d%d%d/n",&p1,&p2,& p3);
node* head = newnode(--p1,--p2,-- p3);
for(int i = 0; i < n; i++ )
for(int j = 0; j < n; j++ )
scanf("%c%*[/n ]",& g[i][j]);
int res = bfs(head,n);
res == -1 ? printf("impossible/n") : printf("%d/n" ,res);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1103.txt","r" ,stdin);
freopen("ans.txt","w" ,stdout);
#endif
int n;
while(scanf("%d",&n) != EOF && n != 0 )
solve(n);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
}