problem 1103

   最基础的广搜题。没有做任何优化,测试数据生成了超过一万五千个结点,效率一般。

Accepted1103C++00:00.02628K

代码相当长,懒的压缩了。。。

#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

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值