afterward

导航

 

 

View Code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 155
using namespace std;

int map[maxn][maxn],res[maxn];
bool vt[maxn];
int n,m;
bool flag;

void dfs(int pos,int num)
{
    int i;
    if (flag) return ;
          res[num] = pos;
    for (i = 1;i <= n;++i)
    {
       if (map[pos][i])
        {
            printf(">>%d\n",i);
            if (!vt[i])
            {
                printf("***%d\n",i);
                vt[i] = true;
                dfs(i,num+1);
                vt[i] = false;
            }
            else if (num+1==n + 1 && i==1)
            {
            flag = true;
            break;
            }
       }
      if (flag) return ;
    }
}
int main()
{
    //freopen("d.txt","r",stdin);
    int i,x,y;
while (~scanf("%d%d",&n,&m))
  {
    flag = false;
    memset(map,0,sizeof(map));
    memset(vt,false,sizeof(vt));

    for (i=0;i<m;++i)
    {
        scanf("%d%d",&x,&y);
        map[x][y] = map[y][x] = 1;
        }
        vt[1] = true;
        dfs(1,1);
        if (flag)
       {
        for (i = 1;i < n;++i)
        printf("%d ",res[i]);
            printf("%d\n",res[i]);
        }
        else
        {
            printf("no solution\n");
        }
    }
    return 0;
}

 

运用了邻接表的深度优先搜索

View Code
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;

struct node{
    int dest;
    node* next;
}s[160];
bool vis[160];
node temp[40000];
stack<int>ans;
int ac,n;

bool dfs(int i)
{
    if(ans.size()==n)
    {
        node* p=s[i].next;
        while(p!=NULL)
        {
            if(p->dest==1)
                return true;
            p=p->next;
        }
        return false;
    }
    node* p=s[i].next;
    while(p!=NULL)
    {
        if(!vis[p->dest])
        {
            vis[p->dest]=true;
            ans.push(p->dest);
            if(dfs(p->dest))
                return true;
            else
            {
                vis[p->dest]=false;
                ans.pop();
            }
        }
        p=p->next;
    }
    return false;
}

void creat(int i,int j)
{
    node* p=&temp[ac++];
    p->dest=j;
    p->next=s[i].next;
    s[i].next=p;
}
int main()
{
    int m,i,j;
    node* p;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            vis[i]=false;
            s[i].next=NULL;
        }
        ac=0;
        while(m--)
        {
            scanf("%d%d",&i,&j);
            creat(i,j);
            creat(j,i);
        }
        ans.push(1);
        vis[1]=true;
        if(dfs(1))
        {
            printf("%d",ans.top());
            ans.pop();
            while(!ans.empty())
            {
                printf(" %d",ans.top());
                ans.pop();
            }
            printf("\n");

        }
        else
        {
            printf("no solution\n");
            while(!ans.empty())
                ans.pop();
        }
    }
    return 0;
}

 

View Code
/*
6 9
1 2
1 3
1 4
2 3
2 4
2 6
3 5
4 5
5 6


1 3 2 6 5 4

*/
#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;
#define V 200
int n,m;
bool c[V][V];
int x[V];
bool flag[V];
void hamilton()
{
    int i, k;
    bool s[V];
    for(i = 0; i < n; i++)
    {
        x[i] = -1;
        s[i] = false;
    }
    k = 1;
    s[0] = true;//记录点x[k]是否被访问过
    x[0] = 0;
    //x[k]一直是++的,找与第k-1相邻的点,不会找重复。
    while(k >= 0)//k表示进去的第k个点
    {
        x[k]++;//从0~n 找与第k-1点相邻的点
        cout<<"---"<<k<<"  ---"<<x[k-1]+1<<" "<<endl;
        while(x[k] < n)
            if(!s[x[k]] && c[x[k - 1]][x[k]])
            {
                cout<<">>>"<<k<<"  >>>"<<x[k]+1<<" "<<s[x[k]]<<endl;
                break;//找到一个相邻点
            }

            else x[k]++;
        if((x[k] < n) && (k != n - 1))
        {
            s[x[k]] = true;
            k++;//继续往下找点
        }
        else if((x[k] < n) && k == n - 1 && c[x[k]][x[0]])
               {
                  break;//找齐n-1个,且最后一个与第一个相邻
               }
             else//没找到下一个没被访问的点;没找齐n-1个 就退一个
             {
               x[k] = -1;
               k--;
               s[x[k]] = false;//删除该点的访问记录
               cout<<"---"<<k<<"  <<<"<<x[k]+1<<" "<<s[x[k]]<<endl;
             }

    }
}
int main()
{
//freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout);
    int a,b;
    while(cin >> n >> m)
    {
        memset(c,0,sizeof(c));
        memset(flag,0,sizeof(flag));
        memset(x,0,sizeof(x));
        for(int i=0; i<m; i++)
        {
            cin >> a >> b;
            c[a-1][b-1]=c[b-1][a-1]=true;
        }
        hamilton();
        bool f=0;
        for(int i=0; i<n; i++)
        {
            flag[x[i]]=1;
        }
        for(int i=0; i<n; i++)
        {
            if(!flag[i]) f=1;
        }
        if(f) cout <<"no solution" <<endl;
        else
        {
            for(int i=0; i<n; i++)
            {
                if(i==n-1)
                    cout << x[i]+1 <<endl;
                else cout << x[i]+1 <<" ";
            }
        }
    }
    return 0;
}

 

 哈密顿

以上均只供学习参考,看代码的日子!

 

King Arthur's Knights

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 897    Accepted Submission(s): 360
Special Judge

Problem Description
I am the bone of my sword. Steel is my body, and the fire is my blood.
- from Fate / Stay Night
You must have known the legend of King Arthur and his knights of the round table. The round table has no head, implying that everyone has equal status. Some knights are close friends with each other, so they prefer to sit next to each other.

Given the relationship of these knights, the King Arthur request you to find an arrangement such that, for every knight, his two adjacent knights are both his close friends. And you should note that because the knights are very united, everyone has at least half of the group as his close friends. More specifically speaking, if there are N knights in total, every knight has at least (N + 1) / 2 other knights as his close friends.
 

Input
The first line of each test case contains two integers N (3 <= N <= 150) and M, indicating that there are N knights and M relationships in total. Then M lines followed, each of which contains two integers ai and bi (1 <= ai, bi <= n, ai != bi), indicating that knight ai and knight bi are close friends.
 

Output
For each test case, output one line containing N integers X1, X2, ..., XN separated by spaces, which indicating an round table arrangement. Please note that XN and X1 are also considered adjacent. The answer may be not unique, and any correct answer will be OK. If there is no solution exists, just output "no solution".
 

Sample Input
3 3
1 2
2 3
1 3
4 4
1 4
2 4
2 3
1 3
 

Sample Output
1 2 3
1 4 2 3

posted on 2012-08-05 17:48  afterward  阅读(358)  评论(0编辑  收藏  举报