图(领接矩阵和vector)的dfs和bfs

//Uva572 图的dfs求连通块

#include
#include
#include
using namespace std;
int m,n;
int dir[8][2] = {{-1,0},{-1,1},{0,1},{1,1},
                 { 1,0},{1,-1},{0,-1},{-1,-1}};
bool vis[105][105];
char map[105][105];

void dfs(int x, int y){
    for(int i=0; i<8; ++i){
        int dx=x+dir[i][0], dy=y+dir[i][1];
        if(dx>=0 && dx=0 && dy             vis[dx][dy] = true;
            dfs(dx, dy);
        }
    }
}

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    int i,j;
    while(scanf("%d %d%*c",&m,&n) && m && n){
        for(i=0; i             gets(map[i]);
        memset(vis, 0, sizeof(vis));
        int cnt=0;
        for(i=0; i             for(j=0; j                 if(map[i][j]=='@' && !vis[i][j]){
                    ++cnt;
                    dfs(i, j);
                }
            }
        }
        printf("%d\n",cnt);
    }   
    
    return 0;
}


//Uva1103

#include
#include
#include
#include
#include
#include

using namespace std;

const int MAXH = 210;
const int MAXW = 100;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
char* bin[]= {"0000","0001","0010","0011","0100","0101","0110","0111",
            "1000","1001","1010","1011","1100","1101","1110","1111"};
char code[] = "WAKJSD";

int W,H;
char pic1[MAXH][MAXW];
char pic[MAXH][MAXW<<2];
int color[MAXH][MAXW<<2];

void dfs(int x, int y, int col)
{
    color[x][y] = col;
    for(int i = 0; i < 4; ++i){
        int xx = x + dx[i], yy = y + dy[i];
        if(xx >= 0 && xx < H && yy >= 0 && yy < W
           && pic[x][y] == pic[xx][yy] && !color[xx][yy]){
            dfs(xx,yy,col);
           }
    }
}

int main(void)
{
    //freopen("input.txt","r",stdin);
    int cas = 1;
    while(scanf("%d %d", &H, &W), H || W){

        memset(pic,0,sizeof(pic));
        memset(color,0,sizeof(color));

        for(int i = 0; i < H; ++i)
            scanf("%s",pic1[i]);
        for(int i = 0 ; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(isdigit(pic1[i][j]))
                    pic1[i][j] -= '0';
                else
                    pic1[i][j] -= 'a' - 10;
                for(int k = 0; k < 4; ++k)
                    pic[i+1][1 + 4 * j + k] = bin[pic1[i][j]][k] - '0';
            }
        }

        H += 2;
        W = 4 * W + 2;
        int cnt = 0;
        vector cc;
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(!color[i][j]){
                    dfs(i,j,++cnt);
                    if(pic[i][j] == 1) cc.push_back(cnt);
                }
            }
        }

        vector > neigh(cnt+1);
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
               if(pic[i][j]){
                    for(int k = 0; k < 4; ++k){
                        int x = i + dx[k], y = j + dy[k];
                        if(x >= 0 && x < H && y >= 0 && y < W &&
                           pic[x][y] == 0 && color[x][y] != 1)
                            neigh[color[i][j]].insert(color[x][y]);
                    }
                }
            }
        }

        vector ans;
        for(int i = 0, sz = cc.size(); i < sz; ++i)
            ans.push_back(code[neigh[cc[i]].size()]);
        sort(ans.begin(),ans.end());

        printf("Case %d: ",cas++);
        for(int i = 0, sz = ans.size(); i < sz; ++i)
            printf("%c",ans[i]);
        puts("");

    }
    return 0;
}

//图的bfs求最短路

#include
#include
#include
#include
#include
#include
using namespace std;

struct note
{
  int r;
  int c;
  int dir;
};
string s,s1;
int r0,c0,r1,c1,dir,r2,c2;
int has_edge[10][10][4][3];
int d[10][10][4];
note p[10][10][4];
char dirs[]={'N','E','S','W'};
char turns[]={'F','L','R'};
int dr[]={-1,0,1,0};
int dc[]={0,1,0,-1};

note walk(note u,int turn)
{
  note v;
  int dirr=u.dir;
  if(turn==1) {dirr=(dirr+3)%4;}
  if(turn==2) {dirr=(dirr+1)%4;}
  v.r=u.r+dr[dirr];
  v.c=u.c+dc[dirr];
  v.dir=dirr;
  return v;
}
bool inside(int x,int y)
{
  if(x>=1&&x<=9&&y>=1&&y<=9) return true;
  return false;
}
void print_ans(note u)
{
  vector vec;
  while(1)
  {
    if(d[u.r][u.c][u.dir]==0) break;
    vec.push_back(u);
    u=p[u.r][u.c][u.dir];
  }
  u.r=r0;u.c=c0;u.dir=dir;
  vec.push_back(u);
  cout<  int cnt=0;
  for(int i=vec.size()-1;i>=0;i--)
  {
    cnt++;
    if(cnt==1) printf("  ");
    printf("(%d,%d)",vec[i].r,vec[i].c);
    if(cnt!=10&&i!=0) printf(" ");
    if(cnt==10&&i!=0) {printf("\n");cnt=0;}
  }
  printf("\n");
}
void solve()
{
  queue q;
  note uu;
  uu.r=r0;uu.c=c0;uu.dir=dir;
  note u;
  u.r=r1;u.c=c1;u.dir=dir;
  d[r1][c1][dir]=1;
  p[r1][c1][dir]=uu;
  d[r0][c0][dir]=0;
  q.push(u);
  while(!q.empty())
  {
    u=q.front();
    q.pop();
    if(u.r==r2&&u.c==c2) {print_ans(u);return;}
    for(int i=0;i<3;i++)
    {
      note v=walk(u,i);
      if(has_edge[u.r][u.c][u.dir][i]&&inside(v.r,v.c)&&d[v.r][v.c][v.dir]<0)
      {
        p[v.r][v.c][v.dir]=u;
        d[v.r][v.c][v.dir]=d[u.r][u.c][u.dir]+1;
        q.push(v);
      }
    }
  }
  cout<  printf("  No Solution Possible\n");
}

int main()
{
  while(cin>>s1)
  {
    if(s1=="END") break;
    memset(has_edge,0,sizeof(has_edge));
    memset(d,-1,sizeof(d));
    memset(p,0,sizeof(p));
    int a,count=0;
    int a1,a2,dir1,turn1;
    while(cin>>a)
    {
      if(a==0) break;
      count++;
      if(count==1)
      {
        r0=a;
        cin>>c0>>s>>r2>>c2;
        for(int i=0;i<4;i++)
        {
          if(s[0]==dirs[i])
          {
            dir=i;
            break;
          }
        }
        r1=r0+dr[dir];
        c1=c0+dc[dir];
      }
      else
      {
        a1=a;
        cin>>a2;
        while(cin>>s)
        {
          if(s[0]=='*') break;
          for(int i=0;i<4;i++)
          {
            if(s[0]==dirs[i])
            {
              dir1=i;
              break;
            }
          }
          for(int i=1;i          {
            for(int j=0;j<3;j++)
            {
              if(s[i]==turns[j])
              {
                turn1=j;
                break;
              }
            }
            has_edge[a1][a2][dir1][turn1]=1;
          }
        }
      }
    }
//		for(int i=1;i<=9;i++)
//		{
//			for(int j=1;j<=9;j++)
//			{
//				for(int k=0;k<4;k++)
//				{
//					for(int l=0;l<3;l++)
//					{
//						if(has_edge[i][j][k][l])
//							printf("[%d][%d][%d][%d]\n",i,j,k,l);
//					}
//				}
//			}
//		}
    solve();
  }
  return 0;
}

//图的拓扑排序DAG(有向图的节点排序问题)排序失败说明不是DAG

//有向图的拓扑排序

//WA..有一个陷阱就是n!=0 &&m==0 的情况

#include

#include

#define MAX 100

int c[MAX];

int topo[MAX],t;

int n,m;

int g[MAX][MAX];

bool dfs(int u)

{

   c[u]=-1;

   for(int v=0;v

       if(g[u][v])

       {

           if(c[v]<0)return false;

           else if(!c[v] && !dfs(v))returnfalse;

       }

   c[u]=1;

   topo[--t]=u;

   return true;

}

bool toposort()

{

   t=n;

   memset(c,0,sizeof(c));

   for(int u=0;u

       if(!c[u] && !dfs(u))returnfalse;

   return true;

}

int main()

{

   int i,j;

   while(scanf("%d%d",&n,&m),n)

   {

       memset(g,0,sizeof(g));

       while(m--)

       {

           scanf("%d%d",&i,&j);

           i--;

           j--;

           g[i][j]=1;

       }

       toposort();

       for(i=0;i

       {

           if(i!=0)printf(" ");

           printf("%d",topo[i]+1);

       }

       printf("\n");

   }

   return 0;

}

//图的欧拉回路

#include
#include
int indgr[30],outdgr[30],p[30];
char b[1010];
int find(int x)
{
    return p[x]==x?x:(p[x]=find(p[x]));
}
int main()
{
    int i,j,first,last,t,n,num,ok;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(indgr,0,sizeof(indgr));
        memset(outdgr,0,sizeof(outdgr));
        for(i=0;i<26;i++)
            p[i]=i;
        for(i=0;i         {
            scanf("%s",b);
            first=b[0]-'a';
            last=b[strlen(b)-1]-'a';
            outdgr[first]++;
            indgr[last]++;
            if(find(first)!=find(last))
                p[find(first)]=find(last);
        }
        num=0;
        ok=1;
        for(i=0;!indgr[i]&&!outdgr[i];i++);
        for(j=i+1;j<26;j++)
            if((indgr[j]||outdgr[j])&&find(i)!=find(j))
                {
                    ok=0;
                    break;
                }
        if(ok)
            for(i=0;i<26;i++)
            {
                if(indgr[i]>outdgr[i])
                {
                    if(indgr[i]-outdgr[i]>1)
                    {
                        ok=0;
                        break;
                    }
                    else
                        num++;
                }
                else if(outdgr[i]>indgr[i])
                {
                    if(outdgr[i]-indgr[i]>1)
                    {
                        ok=0;
                        break;
                    }
                    else
                        num++;
                }
            }
        if(!ok||num>2)
            printf("The door cannot be opened.\n");
        else
            printf("Ordering is possible.\n");
    }
    return 0;
}

你可能感兴趣的:(图(领接矩阵和vector)的dfs和bfs)