1008. Image Encoding(bfs)

1008

没营养的破题

  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<algorithm>

  5 #include<stdlib.h>

  6 #include<queue>

  7 using namespace std;

  8 typedef struct node

  9 {

 10     int x,y;

 11 }st;

 12 int vis[15][15],w[15][15];

 13 int n,o[115],dis[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

 14 int s[115][8],gg;

 15 char ss[115][8];

 16 st p[115];

 17 int judge(int x,int y)

 18 {

 19     if(x<1||x>10||y<1||y>10)

 20     return 0;

 21     if(!w[x][y])

 22     return 0;

 23     return 1;

 24 }

 25 char swit(int x)

 26 {

 27     if(x==0)

 28     return 'R';

 29     else if(x==1)

 30     return 'T';

 31     else if(x==2)

 32     return 'L';

 33     else

 34     return 'B';

 35 }

 36 void bfs()

 37 {

 38     int i,g=0,j;

 39     queue<st>q;

 40     st te,tt;

 41     q.push(p[1]);

 42     vis[p[1].x][p[1].y] = 1;

 43     while(!q.empty())

 44     {

 45         te = q.front();

 46         q.pop();

 47         g++;

 48         for(i = 0 ; i < 4 ; i++)

 49         {

 50             int tx = te.x+dis[i][0];

 51             int ty = te.y+dis[i][1];

 52             if(judge(tx,ty))

 53             {

 54                 if(!vis[tx][ty])

 55                 {

 56                     o[g]++;

 57                     s[g][o[g]] = i;

 58                     vis[tx][ty] = 1;

 59                     tt.x = tx;

 60                     tt.y = ty;

 61                     q.push(tt);

 62                 }

 63             }

 64         }

 65     }

 66     printf("%d %d\n",p[1].x,p[1].y);

 67     for(i =1; i <= n ; i++)

 68     {

 69         for(j = 1 ; j <= o[i] ; j++)

 70         {

 71             printf("%c",swit(s[i][j]));

 72         }

 73         if(i!=n)

 74         printf(",\n");

 75         else

 76         printf(".\n");

 77     }

 78 }

 79 bool cmp(node a,node b)

 80 {

 81     if(a.x==b.x)

 82     return a.y<b.y;

 83     return a.x<b.x;

 84 }

 85 void bfs1(int x,int y)

 86 {

 87     int i,g=0;

 88     queue<st>q;

 89     st te,tt;

 90     te.x = x;

 91     te.y = y;

 92     q.push(te);

 93     vis[x][y] = 1;

 94     while(!q.empty())

 95     {

 96         te = q.front();

 97         q.pop();

 98         g++;

 99         p[g].x = te.x;

100         p[g].y = te.y;

101         int k = strlen(ss[g]);

102         for(i = 0 ; i < k ; i++)

103         {

104             if(ss[g][i]=='.'||ss[g][i]==',')

105             break;

106             int tx,ty;

107             if(ss[g][i]=='R')

108             {

109                 tx = te.x+1;

110                 ty = te.y;

111             }

112             else if(ss[g][i]=='T')

113             {

114                 tx = te.x;

115                 ty = te.y+1;

116             }

117             else if(ss[g][i]=='L')

118             {

119                 tx = te.x-1;

120                 ty = te.y;

121             }

122             else

123             {

124                 tx = te.x;

125                 ty = te.y-1;

126             }

127             if(!vis[tx][ty])

128             {

129                 vis[tx][ty] = 1;

130                 tt.x = tx;

131                 tt.y = ty;

132                 q.push(tt);

133             }

134         }

135     }

136     sort(p+1,p+g+1,cmp);

137     printf("%d\n",g);

138     for(i = 1; i <= g ; i++)

139     printf("%d %d\n",p[i].x,p[i].y);

140 }

141 int main()

142 {

143     int i,j;

144     char c[10],c1[10],c2[10];

145     gets(c);

146     int k = strlen(c);

147     for(i = 0 ; i < k ; i++)

148     {

149         c1[i] = c[i];

150         if(c[i]==' ')

151         break;

152     }

153     c1[i] = '\0';

154     if(i==k)

155     {

156         n = atoi(c);

157         for(i =1; i <= n ;i++)

158         {

159             scanf("%d%d",&p[i].x,&p[i].y);

160             w[p[i].x][p[i].y] = 1;

161         }

162         sort(p+1,p+n+1,cmp);

163         bfs();

164     }

165     else

166     {

167         int oo=0;

168         for(j = i+1 ; j < k ; j++)

169         c2[oo++] = c[j];

170         c2[oo] ='\0';

171         int x = atoi(c1);

172         int y = atoi(c2);

173         gg=1;

174         while(cin>>ss[gg])

175         {

176             if(ss[gg][0]=='.')

177             break;

178             gg++;

179         }

180         bfs1(x,y);

181     }

182     return 0;

183 }
View Code

 

你可能感兴趣的:(encoding)