您的位置:首页 > 其它

uva_208-Firetruck(救火车)

2012-05-20 23:17 381 查看
暴搜直接TLE,但是进行预处理,即只搜索终点所连接到的点,一下子就只跑了8ms,神奇的概率。

搜索2:





#include<cstdio>
#include<cstring> 
#include <vector>
#include<algorithm> 
using namespace std;
int path[21];
vector <int> ok;
bool vis[21];
bool a[21][21];
int x, y, t;
int cnt = 0;
int nCase = 1;
void pre_dfs(int cur)
{
      vis[cur] = true;
      ok.push_back(cur);
      for (int i = 1; i < 21; i++)
          if (!vis[i] && a[cur][i])
             pre_dfs(i);
              
}
               
void dfs( int cur, int len)
{
      int i ;
      if (cur == t)
      {
              cnt++;
              for(i = 0; i < len-1; i++ )
                 printf("%d ", path[i]);
              printf("%d\n", path[i]);
              return;
      }
      for (i = 0; i < ok.size(); i++)
      {
             if (!vis[ok[i]] && a[cur][ok[i]] )
             {
                     path[len] = ok[i];
                     vis[ok[i]] = true;
                     dfs(ok[i], len+1);
                     vis[ok[i]] = false;
             }
      }
} 
             
            
main()
{
     // FILE *fp = fopen("in.txt", "r");
      while(scanf("%d", &t) != EOF) 
      {
              memset(a, false, sizeof(a));
              memset(vis, false, sizeof(vis));
              memset(path, 0, sizeof(path));
              ok.clear();
              while (scanf("%d %d", &x, &y) !=EOF && (x||y))
              {
                     a[x][y] = a[y][x] = true;
              }
              printf("CASE %d:\n", nCase++);
              pre_dfs(t);
              memset(vis, false, sizeof(vis));
              vis[1] = true;
              path[0] = 1;
              cnt = 0;
              
              sort(ok.begin(), ok.end());
              dfs(1, 1);
              printf("There are %d routes from the firestation to streetcorner %d.\n", cnt, t);
      }
     // getchar();
      return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: