Skip to main content
  1. Posts/

poj 2688 Cleaning Robot (tsp问题)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

______

好蠢,竟然没看出来这道题的不同之处,以为就是个搜

然后样例什么的都过了...

结果显然wa…

然后才发现,这道题应该是tsp问题.

解法是先跑一遍bfs,

对于所有的脏点和起点,得到没两个点之间的距离.

然后跑一遍dfs,枚举出所有的组合,同时更新答案.

晚安.

  1/*************************************************************************
  2	> File Name: code/poj/rr2688.cpp
  3	> Author: 111qqz
  4	> Email: rkz2013@126.com
  5	> Created Time: 2015年08月16日 星期日 03时39分34秒
  6 ************************************************************************/
  7
  8#include<iostream>
  9#include<iomanip>
 10#include<cstdio>
 11#include<algorithm>
 12#include<cmath>
 13#include<cstring>
 14#include<string>
 15#include<map>
 16#include<set>
 17#include<queue>
 18#include<vector>
 19#include<stack>
 20#define y0 abc111qqz
 21#define y1 hust111qqz
 22#define yn hez111qqz
 23#define j1 cute111qqz
 24#define tm crazy111qqz
 25#define lr dying111qqz
 26using namespace std;
 27#define REP(i, n) for (int i=0;i<int(n);++i)
 28typedef long long LL;
 29typedef unsigned long long ULL;
 30const int inf = 0x7fffffff;
 31const int N=25;
 32int w,h;
 33char maze[N][N];
 34int dist[N][N];
 35int cnt;//机器人与脏地的个数
 36int tag[N][N];//标记
 37bool vist[N][N];
 38struct node
 39{
 40    int x,y;
 41    int step;
 42    bool ok ()
 43    {
 44	if (x<1||x>h||y<1||y>w||vist[x][y]||maze[x][y]=='x')
 45	    return false;
 46	return true;
 47    }
 48}pos[N*N];
 49
 50node robot;
 51int dir[4][2]={0,-1,0,1,-1,0,1,0};
 52void bfs(node p,int po)
 53{
 54    vist[p.x][p.y]=1;
 55    queue<node>q;
 56    q.push(p);
 57    while(!q.empty())
 58    {
 59        node cur=q.front();
 60        q.pop();
 61        if(maze[cur.x][cur.y]=='o' || maze[cur.x][cur.y]=='*')
 62            dist[po][tag[cur.x][cur.y]]=cur.step;
 63        node next;
 64        next.step=cur.step+1;
 65        for(int i=0;i<4;i++)
 66        {
 67            next.x=cur.x+dir[i][0];
 68            next.y=cur.y+dir[i][1];
 69            if(!next.ok())
 70                continue;
 71            q.push(next);
 72            vist[next.x][next.y]=1;
 73        }
 74    }
 75}
 76
 77int ans=inf;
 78bool vis[N];
 79void dfs(int x,int step,int s)
 80{
 81    if(step==cnt)
 82    {
 83        if(s<ans)
 84            ans=s;
 85        return ;
 86    }
 87    if(s>ans)
 88        return ;
 89    for(int j=1;j<=cnt;j++)
 90    {
 91        if(vis[j])
 92            continue;
 93        vis[j]=1;
 94        dfs(j,step+1,s+dist[x][j]);
 95        vis[j]=0;
 96    }
 97}
 98
 99int main()
100{
101    while(~scanf("%d%d",&w,&h))
102    {
103        if(w==0&&h==0)
104            break;
105       // getchar();
106        cnt=0;
107        memset(pos,0,sizeof(pos));
108        memset(tag,0,sizeof(tag));
109        for(int i=1;i<=h;i++)
110        {
111            scanf("%s",maze[i]+1);
112            for(int j=1;j<=w;j++)
113                if (maze[i][j]=='o')
114                {
115                     pos[++cnt].x=i;
116                    pos[cnt].y=j;
117                    robot.x=i;
118                    robot.y=j;
119                    tag[i][j]=cnt;
120                }
121                else if(maze[i][j]=='*')
122                {
123                     pos[++cnt].x=i;
124                    pos[cnt].y=j;
125                    tag[i][j]=cnt;
126                }
127        }
128        for(int i=1;i<=cnt;i++)
129            for(int j=1;j<=cnt;j++)
130                if(i !=j)
131                    dist[i][j]=inf;
132                else
133                    dist[i][j]=0;
134        for(int i=1;i<=cnt;i++)
135        {
136            memset(vist,0,sizeof(vist));
137            pos[i].step=0;
138            bfs(pos[i],i);
139        }
140        bool flag=1;
141        for(int i=1;i<=cnt && flag;i++)
142            for(int j=1;j<=cnt && flag;j++)
143                if(dist[i][j]==inf)
144                     flag=0;
145
146        if(flag==0)
147        {
148            puts("-1");
149            continue;
150        }
151        memset(vis,0,sizeof(vis));
152        vis[tag[robot.x][robot.y]]=1;
153        ans=inf;
154        dfs(tag[robot.x][robot.y],1,0);
155        printf("%d\n",ans);
156
157    }
158    return 0;
159}

Related

hdu 5305 Friends (dfs)

·1 min
dfs 1A 1/************************************************************************* 2 > File Name: code/whust/#9/K.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年08月05日 星期三 15时02分30秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#define y0 abc111qqz 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define tm crazy111qqz 25#define lr dying111qqz 26using namespace std; 27#define REP(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef unsigned long long ULL; 30const int inf = 0x7fffffff; 31int n ,m,ans; 32int d[50]; 33int on[50],off[50]; 34int u[50],v[50]; 35 36bool ok(int x,int y) 37{ 38 if ( !d[x] && !d[y] && on[x] == off[x] && on[y] == off[y] ) 39 return true; 40 if ( !d[x] && d[y] && on[x] == off[x] ) 41 return true; 42 if ( d[x] && !d[y] && on[y] == off[y] ) 43 return true; 44 if ( d[x] && d[y] ) 45 return true; 46 return false; 47} 48 49void dfs ( int i) 50{ 51 if (i==m) 52 { 53 ans++; 54 return; 55 } 56 int x = u[i]; 57 int y = v[i]; 58 d[x]--; 59 d[y]--; 60 on[x]++; 61 on[y]++; 62 if (ok(x,y)) 63 dfs (i+1); 64 on[x]--;on[y]--; 65 off[y]++;off[x]++; 66 if ( ok( x,y)) 67 dfs (i+1); 68 off[y]--; 69 off[x]--; 70 d[x]++; 71 d[y]++; 72} 73 74int main ( ) 75{ 76 int T; 77 cin>>T; 78 while ( T-- ) 79 { 80 ans = 0; 81 scanf ("%d %d",&n,&m); 82 memset (d,0,sizeof(d)); 83 memset (on,0,sizeof(on)); 84 memset (off,0,sizeof(off)); 85 for ( int i = 0 ; i < m ; i++ ) 86 { 87 scanf ("%d%d",&u[i],&v[i]); 88 d[u[i]]++; 89 d[v[i]]++; 90 } 91 dfs (0); 92 printf ( "%d\n" , ans ); 93 } 94}

uva 12442 . Forwarding Emails

·2 mins
“… so forward this to ten other people, to prove that you believe the emperor has 题意是说发短信,每个人只会给一个人发,问从哪个人开始发,能传到的人最多

I - Fire Game (两个点开始的bfs)

·3 mins
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83084#problem/I I - Fire Game **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

poj 3414 pots (bfs+路径记录)

·1 min
好爽,一遍ac 1 2 3 /************************************************************************* 4 > File Name: code/2015summer/searching/H.cpp 5 > Author: 111qqz 6 > Email: rkz2013@126.com 7 > Created Time: 2015年07月27日 星期一 09时11分28秒 8 ************************************************************************/ 9 10 #include<iostream> 11 #include<iomanip> 12 #include<cstdio> 13 #include<algorithm> 14 #include<cmath> 15 #include<cstring> 16 #include<string> 17 #include<map> 18 #include<set> 19 #include<queue> 20 #include<vector> 21 #include<stack> 22 #define y0 abc111qqz 23 #define y1 hust111qqz 24 #define yn hez111qqz 25 #define j1 cute111qqz 26 #define tm crazy111qqz 27 #define lr dying111qqz 28 using namespace std; 29 #define REP(i, n) for (int i=0;i<int(n);++i) 30 typedef long long LL; 31 typedef unsigned long long ULL; 32 const int N=1E2+5; 33 int A,B,C; 34 int d[N][N]; 35 bool flag; 36 struct node 37 { 38 int d,opt,par,prea,preb; 39 }q[N][N]; 40 41 void print(int x,int y) 42 { 43 // cout<<"x:"<<x<<"y:"<<y<<endl; 44 if (q[x][y].prea!=-1&&q[x][y].preb!=-1) 45 { 46 // cout<<"who is 111qqz"<<endl; 47 print(q[x][y].prea,q[x][y].preb); 48 if (q[x][y].opt==1){ 49 printf("FILL(%d)\n",q[x][y].par); 50 } 51 if (q[x][y].opt==2) 52 { 53 printf("DROP(%d)\n",q[x][y].par); 54 } 55 if (q[x][y].opt==3) 56 { 57 printf("POUR(%d,%d)\n",q[x][y].par,3-q[x][y].par); 58 } 59 } 60 61 } 62 void bfs() 63 { 64 memset(q,-1,sizeof(q)); 65 queue<int>a; 66 queue<int>b; 67 a.push(0); 68 b.push(0); 69 q[0][0].d=0; 70 while (!a.empty()&&!b.empty()) 71 { 72 int av = a.front();a.pop(); 73 int bv = b.front();b.pop(); 74 // cout<<"av:"<<av<<"bv:"<<bv<<endl; 75 if (av==C||bv==C) 76 { 77 flag = true; 78 //cout<<"yeah~~~~~~~~~~~~~~~"<<endl; 79 cout<<q[av][bv].d<<endl; 80 // cout<<"prea:"<<q[av][bv].prea<<"preb:"<<q[av][bv].preb<<endl; 81 print(av,bv); 82 return; 83 } 84 if (av<A&&q[A][bv].d==-1) 85 { 86 q[A][bv].d=q[av][bv].d+1; 87 q[A][bv].opt=1; 88 q[A][bv].par=1; 89 q[A][bv].prea=av; 90 q[A][bv].preb=bv; 91 a.push(A); 92 b.push(bv); 93 } 94 if (av>0&&q[0][bv].d==-1) 95 { 96 q[0][bv].d=q[av][bv].d+1; 97 q[0][bv].opt=2; 98 q[0][bv].par=1; 99 q[0][bv].prea=av; 100 q[0][bv].preb=bv; 101 a.push(0); 102 b.push(bv); 103 104 } 105 if (bv<B&&q[av][B].d==-1) 106 { 107 q[av][B].d=q[av][bv].d+1; 108 q[av][B].opt=1; 109 q[av][B].par=2; 110 q[av][B].prea = av; 111 q[av][B].preb = bv; 112 a.push(av); 113 b.push(B); 114 115 } 116 if (bv>0&&q[av][0].d==-1) 117 { 118 q[av][0].d=q[av][bv].d+1; 119 q[av][0].opt=2; 120 q[av][0].par=2; 121 q[av][0].prea=av; 122 q[av][0].preb=bv; 123 a.push(av); 124 b.push(0); 125 } 126 127 if (av+bv<=B&&q[0][av+bv].d==-1) 128 { 129 q[0][av+bv].d=q[av][bv].d+1; 130 q[0][av+bv].opt=3; 131 q[0][av+bv].par=1; 132 q[0][av+bv].prea=av; 133 q[0][av+bv].preb=bv; 134 a.push(0); 135 b.push(av+bv); 136 } 137 if (av+bv>B&&q[av-(B-bv)][B].d==-1) //把1往2里倒入的两种情况 138 { 139 140 int tmp = av-(B-bv); 141 q[tmp][B].d=q[av][bv].d+1; 142 q[tmp][B].opt=3; 143 q[tmp][B].par=1; 144 q[tmp][B].prea=av; 145 q[tmp][B].preb=bv; 146 a.push(tmp); 147 b.push(B); 148 } 149 150 if (bv+av<=A&&q[av+bv][0].d==-1) 151 { 152 q[av+bv][0].d=q[av][bv].d+1; 153 q[av+bv][0].opt=3; 154 q[av+bv][0].par=2; 155 q[av+bv][0].prea=av; 156 q[av+bv][0].preb=bv; 157 a.push(av+bv); 158 b.push(0); 159 } 160 if (bv+av>A&&q[A][bv-(A-av)].d==-1) 161 { 162 int tmp = bv-(A-av); 163 q[A][tmp].d=q[av][bv].d+1; 164 q[A][tmp].opt=3; 165 q[A][tmp].par=2; 166 q[A][tmp].prea=av; 167 q[A][tmp].preb=bv; 168 a.push(A); 169 b.push(tmp); 170 } 171 } 172 } 173 int main() 174 { 175 176 flag = false; 177 cin>>A>>B>>C; 178 bfs(); 179 if (!flag) 180 { 181 cout<<"impossible"<<endl; 182 } 183 184 185 return 0; 186 }

hdoj 1495 非常可乐(bfs)

·3 mins
非常可乐 # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7194 Accepted Submission(s): 2865 **