HDU 3220 ida* Search | | BFS

Source: Internet
Author: User

A graph of 16 points is given, consisting of 0 and 1, each of which can be optionally connected to two points (one must be 0, one is 1), 0,1, exchange

Ask whether the graph can be changed in 3 steps: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 status

If not, output more


Shape to store the graphics state. ida* Search or BFS.


ida* Search

#include "stdio.h" #include "string.h" Const int b[]={ 0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};const int adj[17][5]={{0,0,0,0,0}, {0,2,3,5 , 9},//1 {0,1,4,6,10},//2 {0,1,4,7,11},//3 {0,2,3,8,12},//4 {0,1,6,7,13},//5 {0,2,5,8,14},//6 {0, 3, 5,8,15},//7 {0,4,6,7,16},//8 {0,1,10,11,13},//9 {0,2,9,12,14},//10 {0,3,9,12,15},//11 {0,4,10,11,16}, {0,5,9,14,15},//13 {0,6,10,13,16},//14 {0,7,11,13,16},//15 {0,8,12,14,15}//16};int ans,flag,aim;int m    Ark[70010];int cal (int x) {int i,cnt;    cnt=0;    for (i=1;i<=8;i++) if ((x&b[i))!=0) cnt++; return CNT;}    void Dfs (int cur,int n) {int next,x,cnt,i,j;        if (Cur==aim) {flag=1;    return;    } if (flag==1) return;    cnt=0;    for (i=1;i<=8;i++) if ((cur&b[i))!=0) cnt++;    if (N+cnt>ans) return;        for (i=1;i<=16;i++) {x=cur&b[i]; if (x!=0) for(j=1;j<=4;j++)            {x=cur&b[adj[i][j]];                if (x==0) {next=cur;                Next^=b[i];                NEXT^=B[ADJ[I][J]];                    if (mark[next]==0) {mark[next]=1;                    DFS (NEXT,N+1);                mark[next]=0;    }}}}}int main () {int t,case,i,status,cnt,x;    scanf ("%d", &t);    case=1;    memset (Mark,0,sizeof (Mark));        while (t--) {status=aim=cnt=0;            for (i=1;i<=16;i++) {scanf ("%d", &x);            if (i<=8) cnt+=x;        Status+=x*b[i];        } for (i=9;i<=16;i++) aim+=b[i];        printf ("Case #%d:", case++);            if (aim==status) {printf ("0\n");        Continue            } if (cnt>3) {printf ("more\n");        Continue        } ans=0;         while (1) {ans++;   flag=0;            if (ans==4) break;            Mark[status]=1;            DFS (status,0);            mark[status]=0;        if (flag==1) break;        } if (ans==4) printf ("more\n");    else printf ("%d\n", ans); } return 0;}


BFS

#include "stdio.h" #include "string.h" #include "queue" using namespace Std;const int b[]={ 0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};const int adj[17][5]={{0,0,0,0,0}, {0,2,3,5 , 9},//1 {0,1,4,6,10},//2 {0,1,4,7,11},//3 {0,2,3,8,12},//4 {0,1,6,7,13},//5 {0,2,5,8,14},//6 {0, 3, 5,8,15},//7 {0,4,6,7,16},//8 {0,1,10,11,13},//9 {0,2,9,12,14},//10 {0,3,9,12,15},//11 {0,4,10,11,16}, {0,5,9,14,15},//13 {0,6,10,13,16},//14 {0,7,11,13,16},//15 {0,8,12,14,15}//16};int ans,flag,aim;int m    Ark[70010];int cal (int x) {int i,cnt;    cnt=0;    for (i=1;i<=8;i++) if ((x&b[i))!=0) cnt++; return CNT;}    int BFS (int cur) {int i,j,next;    queue<int>q;    Q.push (cur);    memset (Mark,-1,sizeof (Mark));    mark[cur]=0;        while (!q.empty ()) {Cur=q.front ();        Q.pop ();        if (mark[cur]==3) continue; for (i=1;i<=16;i++) if ((cur&b[i))!=0) for (j=1;j<=4;j++) if ((Cur&b[adj[i][j]) ==0) {next=cur;                Next^=b[i];                NEXT^=B[ADJ[I][J]];                    if (Mark[next]==-1 && cal (next) +mark[cur]<=3) {mark[next]=mark[cur]+1;                    if (Next==aim) return mark[next];                Q.push (next); }}} return-1;}    int main () {int t,case,i,status,cnt,x;    scanf ("%d", &t);    case=1;    aim=0;    for (i=9;i<=16;i++) aim+=b[i];        while (t--) {status=cnt=0;            for (i=1;i<=16;i++) {scanf ("%d", &x);            if (i<=8) cnt+=x;        Status+=x*b[i];        } printf ("Case #%d:", case++);            if (aim==status) {printf ("0\n");        Continue            } if (cnt>3) {printf ("more\n");        Continue        } ans=bfs (status);      if (ans==-1)      printf ("more\n");    else printf ("%d\n", ans); } return 0;}




HDU 3220 ida* Search | | BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.