做做

Aizu 0558

#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int, int> P;

const int INF=9999999;
char map[1005][1005];
int d[1005][1005];
int n, m, xx;
int res;
int sx, sy;
int gx, gy;
int x[10];
int y[10];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
queue<P> s;
int bfs()
{
    while(!s.empty()){
        s.pop();
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
          d[i][j]=INF;
    s.push(P(sx, sy));
    d[sx][sy]=0;
    while(!s.empty())
    {
        P tmp= s.front();
        s.pop();
        if(tmp.first==gx && tmp.second==gy)
        {
            break;
        }
        for(int i=0; i<4; i++)
        {
            int nx = tmp.first+dx[i];
            int ny = tmp.second+dy[i];
            if(nx>=0 && nx<n && ny>=0 && ny<m && map[nx][ny]!='X' &&d[nx][ny]==INF)
            {
                s.push(P(nx, ny));
                d[nx][ny]=d[tmp.first][tmp.second]+1;
            }
        }
    }
    return d[gx][gy];
}

int main()
{
    while(scanf("%d%d%d", &n, &m, &xx)!=EOF)
    {
        for(int i=0; i<n; i++)
            scanf("%s", map[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
              if(map[i][j]=='S')
              {
                  sx=i; sy=j;
              }
              if(map[i][j]=='1')
              {
                  x[1]=i;
                  y[1]=j;
              }
              if(map[i][j]=='2')
              {
                  x[2]=i;
                  y[2]=j;
              }
              if(map[i][j]=='3')
              {
                  x[3]=i;
                  y[3]=j;
              }
              if(map[i][j]=='4')
              {
                  x[4]=i;
                  y[4]=j;
              }
              if(map[i][j]=='5')
              {
                  x[5]=i;
                  y[5]=j;
              }
              if(map[i][j]=='6')
              {
                  x[6]=i;
                  y[6]=j;
              }
              if(map[i][j]=='7')
              {
                  x[7]=i;
                  y[7]=j;
              }
              if(map[i][j]=='8')
              {
                  x[8]=i;
                  y[8]=j;
              }
              if(map[i][j]=='9')
              {
                  x[9]=i;
                  y[9]=j;
              }

            }
            res=0;
            gx=x[1]; gy=y[1];
            res+=bfs();
            for(int i=2; i<=xx; i++)
            {
                sx=gx; sy=gy;
                gx=x[i]; gy=y[i];
                res+=bfs();
            }
        printf("%d\n", res);

    }
    return 0;
}


POJ 3669

#include<cstdio>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

struct Node{
  int x, y, t;
};
int vis[305][305];
int dx[]={0,0,-1,0,1};
int dy[]={0,1,0,-1,0};
queue<Node> q;

int bfs()
{
    while(!q.empty()){
        q.pop();
    }
    Node tmp, tmp1;
    tmp.x=tmp.y=tmp.t=0;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        for(int i=1; i<5; i++)
        {
            tmp1.x=tmp.x+dx[i];
            tmp1.y=tmp.y+dy[i];
            tmp1.t=tmp.t+1;
            if(tmp1.x<0 || tmp1.y<0) continue;
            if(vis[tmp1.x][tmp1.y]==-1)return tmp1.t;
            if(tmp1.t>=vis[tmp1.x][tmp1.y]) continue;
            vis[tmp1.x][tmp1.y]=0;
            q.push(tmp1);
        }

    }
    return -1;
}

int main()
{
    int M, x, y, t;
    while(scanf("%d", &M)!=EOF)
    {
        memset(vis, -1, sizeof(vis));
        while(M--)
        {
            scanf("%d%d%d", &x, &y, &t);
            for(int i=0; i<5; i++)
            {
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx>=0 &&nx<=300 &&ny>=0 &&ny<=300)
                    if(vis[nx][ny]==-1) vis[nx][ny]=t;
                    else vis[nx][ny]=min(vis[nx][ny], t);
            }
        }
                    if(vis[0][0]==0) printf("-1\n");
            else if(vis[0][0]==-1) printf("0\n");
            else printf("%d\n", bfs());
    }

    return 0;
}


Aizu 0121

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;

map<string, int> dp;
int dir[4]={1,-1,4,-4};

void bfs()
{
    queue<string> q;
    q.push("01234567");
    dp["01234567"]=0;
    while(!q.empty())
    {
      string now = q.front();
      q.pop();
      int p=0;
      for(int j=0; j<8; j++)
      {
          if(now[j]=='0')
          {
              p=j; break;
          }
      }
      for(int i=0; i<4; i++)
      {
          int n=p+dir[i];
          if(n>=0 &&n<8 && !(p==3&&i==0)&&!(p==4&&i==1))
          {
              string next = now;
              swap(next[p], next[n]);
              if(dp.find(next)==dp.end())
              {
                  dp[next]=dp[now]+1;
                  q.push(next);
              }
          }
      }
    }
}


int main()
{
    bfs();
    string s;
    while(getline(cin, s))
    {
        s.erase(remove(s.begin(), s.end(), ' '), s.end());
        cout<<dp[s]<<endl;
    }

}


POJ 2718

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;


int main()
{
    int perm[10];
    char s[25];
    int n, num1, num2, ans, res, t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        n=0;
        gets(s);
        for(int i=0; s[i]!='\0'; i++)
        {
            if(s[i]>='0' &&s[i]<='9')
                perm[n++]=s[i]-'0';
        }
        if(n==2)
        {
            printf("%d\n", perm[1]-perm[0]);
            continue;
        }
        if(n==3)
        {
            if(perm[0]==0)
                printf("%d\n", perm[1]*10-perm[2]);
            else
                printf("%d\n", perm[0]*10+perm[1]-perm[2]);
            continue;
        }
        while(perm[0]==0)
            next_permutation(perm, perm+n);
        ans=99999999;
        int mid=(n+1)/2;
        if(perm[mid])
        {
            num1=num2=0;
            for(int i=0; i<mid; i++)
                num1=num1*10+perm[i];
            for(int i=mid; i<n; i++)
                num2=num2*10+perm[i];
            res=abs(num1-num2);
            if(ans>res) ans=res;
        }
        while(next_permutation(perm, perm+n))
        {
            int mid=(n+1)/2;
            if(perm[mid])
            {
                num1=num2=0;
                for(int i=0; i<mid; i++)
                    num1=num1*10+perm[i];
                for(int i=mid; i<n; i++)
                    num2=num2*10+perm[i];
                res=abs(num1-num2);
                if(ans>res) ans=res;
            }
        }

        printf("%d\n", ans);

    }


    return 0;
}


POJ 3187

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int b[10][10]={{1},{1, 1},{1, 2, 1},{1, 3, 3, 1},{1, 4, 6, 4, 1 },{1, 5, 10, 10, 5, 1},{1, 6, 15, 20, 15, 6, 1},{1, 7, 21, 35, 35, 21, 7, 1},{1, 8, 28, 56, 70, 56, 28, 8, 1},{1, 9, 36, 84, 126, 126, 84, 36, 9, 1}};




int main()
{
    int a[10];
    int n, sum;
    while(~scanf("%d%d", &n, &sum))
    {

        for(int i=0; i<n; i++)
            a[i]=i+1;
        do{
           int ans=0;
           for(int i=0; i<n; i++)
            ans+=a[i]*b[n-1][i];
           if(ans==sum)
            break;
        }while(next_permutation(a, a+n));
        for(int i=0; i<n; i++)
            printf("%d ", a[i]);
        printf("\n");

    }
    return 0;
}


POJ 3050

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
using namespace std;

map<string, int> dp;
queue<string> q;
int a[5][5];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

void dfs(int x, int y, int k, string s)
{
     if(k==6)
     {
         if(dp.find(s)==dp.end())
            dp[s]=1;
         return;
     }
          string tmp;
     for(int i=0; i<4; i++)
     {
         int nx=x+dx[i];
         int ny=y+dy[i];
         if(nx>=0 &&nx<5 &&ny>=0 &&ny<5)
         {
             tmp=s;
             tmp+=a[nx][ny]+'0';
             dfs(nx, ny, k+1, tmp);
         }
     }

}

int main()
{
    string s;
    while(~scanf("%d", &a[0][0])){

    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
        {
          if(i==0 &&j==0)continue;
          scanf("%d", &a[i][j]);
        }
     dp.clear();
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
        {
          while(!s.empty())s.clear();
          s+=a[i][j]+'0';
          dfs(i, j, 1, s);
        }
        printf("%d\n", dp.size());
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值