简要题意及解析

1001

第三次作业原题,略。

1002

把一个数转换为二进制。

不断除以\(2\)取余就好了。写递归代码会非常短。

时间复杂度\(O(Tlogn)\)

\(T\)是数据组数,\(n\)如题意。

1003

第二次作业原题,略。

1004

有三个容量分别为\(S,N,M\)的容器,初始状态\(S\)装满可乐,\(N,M\)为空。三个容器之间可以互相倒可乐,不过三个容器都没有刻度,也就是说若\(a\)向\(b\)中倒可乐,必须把\(a\)倒空或者把\(b\)倒满。求最少倒几次才能把\(S\)中的可乐平分。平分的定义是:三个容器其中一个为空,另外两个所装可乐相等。不能中途把可乐喝掉。。。

当前局面的状态可以用三个容器中的可乐量,也就是一个三元组\((a,b,c)\)表示。我们从初状态\((S,0,0)\)开始\(bfs\),第一次到达平分状态所需步数就是答案。注意之前搜索到的状态不需要再次搜索,要开一个标记数组。

因此最差情况为每个状态都搜索一次。

时间复杂度\(O(TSNM)\)。

\(T\)为数据组数,\(S,N,M\)如题意。

1005

迷宫问题变形。这个迷宫有两层,你在某些位置会切换当前所在层。

做法和普通迷宫区别不大,用三元组——当前层数、\(x\)坐标和\(y\)坐标表示当前位置即可。

题目有一些坑,到达\(\#\)时必须切换所在层,要判断切换后直接到达终点,如果切换后碰到墙会撞死、切换后碰到\(\#\)会死循环。所以如果切换后碰到\(*\)或\(\#\)一定不能去这个切换点。

时间复杂度\(O(Tnm)\)

\(T\)为数据组数,\(n,m\)如题意。

1006

迷宫问题变形。迷宫变成了三维。

坐标变成三维,方向变成\(6\)种,其他照旧\(bfs\)。

时间复杂度\(O(TABC)\)

\(T\)为数据组数,\(A,B,C\)如题意。

1007

最大团问题。

给出一个图,找一个它的子图使得这个子图是完全图,求这个子图最多有几个点。

暴力搜索每个点要还是不要,需要优化剪枝。

在加入点的时候需要判断加入后是否是完全图,不是的话就不用加了——可行性剪枝。如果当前点数加未搜索点数不能超过最优方案,不用继续搜索了——最优化剪枝。

另外,可以把每个点的连边情况用二进制数表示,压在一个\(long~~long\)里面,这样做的好处是可以快速判断加入一个点后是否是完全图。

时间复杂度\(O(T2^n)\),实际复杂度远低于上界。

\(T\)为数据组数,\(n\)为点数。

1008

地图上有最多\(100\)个数量相等的\(m\)和\(H\),要把所有\(m\)分别送到不同的\(H\)处,代价是两个点的曼哈顿距离。求最小代价。

暴力枚举每个\(m\)要去哪个\(H\),感觉会超时。或许有一些特殊的姿势可以通过。

其实这是一个二分图最小权匹配问题。从每个\(m\)向\(H\)连边,边权为曼哈顿距离。最小权匹配即为答案。

我用费用流做了。

时间复杂度\(O(Tk^5)\)

\(T\)为数据组数,\(k\)为\(m\)的数量。

1009

给出\(n\)个仅含\(A,G,C,T\)的串,构造一个仅含\(A,G,C,T\)的串使得这\(n\)个串都是它的子序列。求构造串最小长度。

求最小步数用\(bfs\),当前状态用\(n\)个数——构造串覆盖了这\(n\)个串的前几个表示。\(bfs\)的时候通过枚举构造串的下一个字符是什么来进行转移。注意搜索过得状态不用再搜索。

时间复杂度\(O(TL^n)\)

\(T\)为数据组数,\(n\)如题意,\(L\)为每个串的长度。

1010

在一个\(M*N\)的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子。求最少步数。

最小步数用\(bfs\),当前状态用搬运工和箱子的坐标这个四元组表示。搬运工可以向四周移动,如果搬运工与箱子重合,视为搬运工推动了一次箱子。模拟进行状态转移即可。搜索过的状态不用再搜索。

时间复杂度\(O(TN^2M^2)\)

\(T\)为数据组数,\(N,N\)如题意。

代码警告

代码警告

代码警告

代码警告

代码警告

代码警告

代码合集:

  1. //1001
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<cstdlib>
  7. #include<algorithm>
  8. #include<cmath>
  9. #include<string>
  10. #include<queue>
  11. #include<vector>
  12. #include<map>
  13. #include<set>
  14. #include<utility>
  15. #include<iomanip>
  16. using namespace std;
  17. int read(){
  18. int xx=0,ff=1;char ch=getchar();
  19. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  20. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  21. return xx*ff;
  22. }
  23. long long READ(){
  24. long long xx=0,ff=1;char ch=getchar();
  25. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  26. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  27. return xx*ff;
  28. }
  29. char one(){
  30. char ch=getchar();
  31. while(ch==' '||ch=='\n')
  32. ch=getchar();
  33. return ch;
  34. }
  35. const int maxn=105,ws_[4]={0,1,0,-1},ad_[4]={1,0,-1,0};
  36. int N,M;
  37. char mp[maxn][maxn];
  38. struct status{
  39. int x,y,t;
  40. status(int x=0,int y=0,int t=0){
  41. this->x=x,this->y=y,this->t=t;
  42. }
  43. bool friend operator<(const status&A,const status&B){
  44. return A.t>B.t;
  45. }
  46. }from[maxn][maxn];
  47. priority_queue<status>pq;
  48. int vis[maxn][maxn];
  49. inline bool out(int x,int y){
  50. return x<=0||y<=0||x>N||y>M;
  51. }
  52. void bfs(){
  53. memset(vis,10,sizeof(vis));
  54. pq.push(status(1,1,0));
  55. vis[1][1]=0;
  56. while(!pq.empty()){
  57. status s=pq.top();
  58. pq.pop();
  59. for(int i=0;i<4;i++){
  60. int tx=s.x+ws_[i],ty=s.y+ad_[i],tt=s.t+1;
  61. if(out(tx,ty)||mp[tx][ty]=='X')
  62. continue;
  63. if(mp[tx][ty]>='1'&&mp[tx][ty]<='9')
  64. tt+=mp[tx][ty]-'0';
  65. if(tt<vis[tx][ty]){
  66. vis[tx][ty]=tt;
  67. pq.push(status(tx,ty,tt));
  68. from[tx][ty]=s;
  69. }
  70. }
  71. }
  72. }
  73. void dfs(int x,int y){
  74. if(x==1&&y==1)
  75. return;
  76. status pre=from[x][y];
  77. dfs(pre.x,pre.y);
  78. printf("%ds:(%d,%d)->(%d,%d)\n",pre.t+1,pre.x-1,pre.y-1,x-1,y-1);
  79. if(mp[x][y]>='1'&&mp[x][y]<='9')
  80. for(int i=1;i<=mp[x][y]-'0';i++)
  81. printf("%ds:FIGHT AT (%d,%d)\n",pre.t+i+1,x-1,y-1);
  82. }
  83. int main(){
  84. //freopen("in","r",stdin);
  85. while(scanf("%d%d",&N,&M)!=EOF){
  86. for(int i=1;i<=N;i++)
  87. for(int j=1;j<=M;j++)
  88. mp[i][j]=one();
  89. bfs();
  90. if(vis[N][M]==vis[0][0])
  91. puts("God please help our poor hero.");
  92. else{
  93. printf("It takes %d seconds to reach the target position, let me show you the way.\n",vis[N][M]);
  94. dfs(N,M);
  95. }
  96. puts("FINISH");
  97. }
  98. return 0;
  99. }
  100. //1002
  101. #include<iostream>
  102. #include<cstdio>
  103. #include<cstring>
  104. #include<ctime>
  105. #include<cstdlib>
  106. #include<algorithm>
  107. #include<cmath>
  108. #include<string>
  109. #include<queue>
  110. #include<vector>
  111. #include<map>
  112. #include<set>
  113. #include<utility>
  114. #include<iomanip>
  115. using namespace std;
  116. int read(){
  117. int xx=0,ff=1;char ch=getchar();
  118. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  119. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  120. return xx*ff;
  121. }
  122. long long READ(){
  123. long long xx=0,ff=1;char ch=getchar();
  124. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  125. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  126. return xx*ff;
  127. }
  128. char one(){
  129. char ch=getchar();
  130. while(ch==' '||ch=='\n')
  131. ch=getchar();
  132. return ch;
  133. }
  134. void trans(int x){
  135.     if(!x)return;
  136.     trans(x/2);
  137.     printf("%d",x%2);
  138. }
  139. int main(){
  140. //freopen("in","r",stdin);
  141.     int n;
  142.     while(scanf("%d",&n)!=EOF){
  143.         trans(n);
  144.         puts("");
  145.     }
  146. return 0;
  147. }
  148. //1003
  149. #include<iostream>
  150. #include<cstdio>
  151. #include<cstring>
  152. #include<ctime>
  153. #include<cstdlib>
  154. #include<algorithm>
  155. #include<cmath>
  156. #include<string>
  157. #include<queue>
  158. #include<vector>
  159. #include<map>
  160. #include<set>
  161. #include<utility>
  162. #include<iomanip>
  163. using namespace std;
  164. int read(){
  165. int xx=0,ff=1;char ch=getchar();
  166. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  167. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  168. return xx*ff;
  169. }
  170. long long READ(){
  171. long long xx=0,ff=1;char ch=getchar();
  172. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  173. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  174. return xx*ff;
  175. }
  176. char one(){
  177. char ch=getchar();
  178. while(ch==' '||ch=='\n')
  179. ch=getchar();
  180. return ch;
  181. }
  182. const int maxn=10010;
  183. int N,M,w[maxn];
  184. double v[maxn];
  185. double f[maxn];
  186. int main(){
  187. //freopen("in","r",stdin);
  188. while(1){
  189. N=read(),M=read();
  190. if(N+M==0)break;
  191. for(int i=0;i<=N;i++)
  192. f[i]=-1;
  193. for(int i=1;i<=M;i++)
  194. w[i]=read(),scanf("%lf",&v[i]);
  195. f[0]=0;
  196. for(int i=1;i<=M;i++)
  197. for(int j=N;j>=w[i];j--)
  198. if(f[j-w[i]]>=0)
  199. f[j]=max(f[j],f[j-w[i]]+(1-f[j-w[i]])*v[i]);
  200. double ans=0;
  201. for(int i=0;i<=N;i++)
  202. ans=max(ans,f[i]);
  203. printf("%.1lf%%\n",ans*100);
  204. }
  205. return 0;
  206. }
  207. //1004
  208. #include<iostream>
  209. #include<cstdio>
  210. #include<cstring>
  211. #include<ctime>
  212. #include<cstdlib>
  213. #include<algorithm>
  214. #include<cmath>
  215. #include<string>
  216. #include<queue>
  217. #include<vector>
  218. #include<map>
  219. #include<set>
  220. #include<utility>
  221. #include<iomanip>
  222. using namespace std;
  223. int read(){
  224. int xx=0,ff=1;char ch=getchar();
  225. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  226. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  227. return xx*ff;
  228. }
  229. long long READ(){
  230. long long xx=0,ff=1;char ch=getchar();
  231. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  232. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  233. return xx*ff;
  234. }
  235. char one(){
  236. char ch=getchar();
  237. while(ch==' '||ch=='\n')
  238. ch=getchar();
  239. return ch;
  240. }
  241. struct my{
  242. int x,y,s;
  243. my(int xx=0,int yy=0,int ss=0){
  244. x=xx,y=yy,s=ss;
  245. }
  246. };
  247. queue<my>q;
  248. bool vis[110][110];
  249. bool check(int x,int y,int z){
  250. if(!x)return y==z;
  251. else if(!y)return x==z;
  252. else if(!z)return x==y;
  253. return 0;
  254. }
  255. int bfs(int a,int b,int c){
  256. memset(vis,0,sizeof(vis));
  257.     while(!q.empty())q.pop();
  258. q.push(my(0,0,0));
  259. vis[0][0]=1;
  260. while(!q.empty()){
  261. my tmp=q.front();
  262. q.pop();
  263. int rem=a-tmp.x-tmp.y;
  264. if(check(tmp.x,tmp.y,rem))
  265. return tmp.s;
  266. int t=min(b-tmp.x,rem);
  267. if(!vis[tmp.x+t][tmp.y]){
  268. vis[tmp.x+t][tmp.y]=1;
  269. q.push(my(tmp.x+t,tmp.y,tmp.s+1));
  270. }//a to b
  271. if(!vis[0][tmp.y]){
  272. vis[0][tmp.y]=1;
  273. q.push(my(0,tmp.y,tmp.s+1));
  274. }//b to a
  275. t=min(c-tmp.y,rem);
  276. if(!vis[tmp.x][tmp.y+t]){
  277. vis[tmp.x][tmp.y+t]=1;
  278. q.push(my(tmp.x,tmp.y+t,tmp.s+1));
  279. }//a to c
  280. if(!vis[tmp.x][0]){
  281. vis[tmp.x][0]=1;
  282. q.push(my(tmp.x,0,tmp.s+1));
  283. }//c to a
  284. t=min(tmp.x,c-tmp.y);
  285. if(!vis[tmp.x-t][tmp.y+t]){
  286. vis[tmp.x-t][tmp.y+t]=1;
  287. q.push(my(tmp.x-t,tmp.y+t,tmp.s+1));
  288. }//b to c
  289. t=min(b-tmp.x,tmp.y);
  290. if(!vis[tmp.x+t][tmp.y-t]){
  291. vis[tmp.x+t][tmp.y-t]=1;
  292. q.push(my(tmp.x+t,tmp.y-t,tmp.s+1));
  293. }//c to b
  294. }
  295. return -1;
  296. }
  297. int main(){
  298. //freopen("in","r",stdin);
  299. while(1){
  300. int a=read(),b=read(),c=read();
  301. if(a+b+c==0)break;
  302. int res=bfs(a,b,c);
  303. if(res==-1)puts("NO");
  304. else printf("%d\n",res);
  305. }
  306. return 0;
  307. }
  308. //1005
  309. #include<iostream>
  310. #include<cstdio>
  311. #include<cstring>
  312. #include<ctime>
  313. #include<cstdlib>
  314. #include<algorithm>
  315. #include<cmath>
  316. #include<string>
  317. #include<queue>
  318. #include<vector>
  319. #include<map>
  320. #include<set>
  321. #include<utility>
  322. #include<iomanip>
  323. using namespace std;
  324. int read(){
  325. int xx=0,ff=1;char ch=getchar();
  326. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  327. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  328. return xx*ff;
  329. }
  330. long long READ(){
  331. long long xx=0,ff=1;char ch=getchar();
  332. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  333. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  334. return xx*ff;
  335. }
  336. char one(){
  337. char ch=getchar();
  338. while(ch==' '||ch=='\n')
  339. ch=getchar();
  340. return ch;
  341. }
  342. const int maxn=12;
  343. const int ws_[4]={1,0,-1,0},ad_[4]={0,1,0,-1};
  344. int N,M,T;
  345. char mp[2][maxn][maxn];
  346. struct pos{
  347. int z,x,y,v;
  348. pos(int zz=0,int xx=0,int yy=0,int vv=0){
  349. z=zz,x=xx,y=yy,v=vv;
  350. }
  351. };
  352. queue<pos>q;
  353. bool vis[2][maxn][maxn];
  354. inline bool out(int x,int y){
  355. return x<1||y<1||x>N||y>M;
  356. }
  357. bool bfs(){
  358. memset(vis,0,sizeof(vis));
  359. while(!q.empty())q.pop();
  360. q.push(pos(0,1,1,0));
  361.     vis[0][1][1]=1;
  362. while(!q.empty()){
  363. pos p=q.front();
  364. q.pop();
  365.         if(mp[p.z][p.x][p.y]=='P')
  366.             return p.v<=T;
  367.         if(p.v>T)break;
  368. for(int i=0;i<4;i++){
  369. int tx=p.x+ws_[i],ty=p.y+ad_[i];
  370. if(out(tx,ty))continue;
  371. if(mp[p.z][tx][ty]=='*')continue;
  372. if(mp[p.z][tx][ty]=='#'){
  373. if(!vis[!p.z][tx][ty]&&mp[!p.z][tx][ty]!='*'&&mp[!p.z][tx][ty]!='#'){
  374. vis[!p.z][tx][ty]=1;
  375. q.push(pos(!p.z,tx,ty,p.v+1));
  376. }
  377. continue;
  378. }
  379. if(!vis[p.z][tx][ty]){
  380. vis[p.z][tx][ty]=1;
  381. q.push(pos(p.z,tx,ty,p.v+1));
  382. }
  383. }
  384. }
  385. return 0;
  386. }
  387. int main(){
  388. //freopen("in","r",stdin);
  389. for(int _=read();_;_--){
  390. N=read(),M=read(),T=read();
  391. for(int k=0;k<=1;k++)
  392. for(int i=1;i<=N;i++)
  393. for(int j=1;j<=M;j++)
  394. mp[k][i][j]=one();
  395. if(bfs())puts("YES");
  396. else puts("NO");
  397. }
  398. return 0;
  399. }
  400. //1006
  401. #include<iostream>
  402. #include<cstdio>
  403. #include<cstring>
  404. #include<ctime>
  405. #include<cstdlib>
  406. #include<algorithm>
  407. #include<cmath>
  408. #include<string>
  409. #include<queue>
  410. #include<vector>
  411. #include<map>
  412. #include<set>
  413. #include<utility>
  414. #include<iomanip>
  415. using namespace std;
  416. int read(){
  417. int xx=0,ff=1;char ch=getchar();
  418. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  419. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  420. return xx*ff;
  421. }
  422. long long READ(){
  423. long long xx=0,ff=1;char ch=getchar();
  424. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  425. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  426. return xx*ff;
  427. }
  428. char one(){
  429.     char ch=getchar();
  430.     while(ch==' '||ch=='\n')
  431.         ch=getchar();
  432.     return ch;
  433. }
  434. const int maxn=52;
  435. const int ws_[6]={1,-1,0,0,0,0},ad_[6]={0,0,1,-1,0,0},qe_[6]={0,0,0,0,1,-1};
  436. int N,M,K,T;
  437. int mp[maxn][maxn][maxn];
  438. struct pos{
  439.     int x,y,z,v;
  440.     pos(int xx=0,int yy=0,int zz=0,int vv=0){
  441.         x=xx,y=yy,z=zz,v=vv;
  442.     }
  443. };
  444. queue<pos>q;
  445. bool vis[maxn][maxn][maxn];
  446. inline bool out(int x,int y,int z){
  447.     return x<1||y<1||z<1||x>K||y>N||z>M;
  448. }
  449. int bfs(){
  450.     memset(vis,0,sizeof(vis));
  451.     while(!q.empty())q.pop();
  452.     q.push(pos(1,1,1,0));
  453.     while(!q.empty()){
  454.         pos p=q.front();
  455.         q.pop();
  456.         for(int i=0;i<6;i++){
  457.             int tx=p.x+ws_[i],ty=p.y+ad_[i],tz=p.z+qe_[i];
  458.             if(out(tx,ty,tz))continue;
  459.             if(mp[tx][ty][tz])continue;
  460.             if(vis[tx][ty][tz])continue;
  461.             if(tx==K&&ty==N&&tz==M){
  462.                 if(p.v+1<=T)return p.v+1;
  463.                 else return -1;
  464.             }
  465.             vis[tx][ty][tz]=1;
  466.             q.push(pos(tx,ty,tz,p.v+1));
  467.         }
  468.     }
  469.     return -1;
  470. }
  471. int main(){
  472.     //freopen("in","r",stdin);
  473.     for(int _=read();_;_--){
  474.         K=read(),N=read(),M=read(),T=read();
  475.         for(int k=1;k<=K;k++)
  476.             for(int i=1;i<=N;i++)
  477.                 for(int j=1;j<=M;j++)
  478.                     mp[k][i][j]=read();
  479.         printf("%d\n",bfs());
  480.     }
  481.     return 0;
  482. }
  483. //1007
  484. #include<iostream>
  485. #include<cstdio>
  486. #include<cstring>
  487. #include<ctime>
  488. #include<cstdlib>
  489. #include<algorithm>
  490. #include<cmath>
  491. #include<string>
  492. #include<queue>
  493. #include<vector>
  494. #include<map>
  495. #include<set>
  496. #include<utility>
  497. #include<iomanip>
  498. using namespace std;
  499. int read(){
  500. int xx=0,ff=1;char ch=getchar();
  501. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  502. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  503. return xx*ff;
  504. }
  505. long long READ(){
  506. long long xx=0,ff=1;char ch=getchar();
  507. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  508. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  509. return xx*ff;
  510. }
  511. char one(){
  512. char ch=getchar();
  513. while(ch==' '||ch=='\n')
  514. ch=getchar();
  515. return ch;
  516. }
  517. const int maxn=52;
  518. int N,ans,a[maxn],cnt,deg[maxn];
  519. bool e[maxn][maxn];
  520. long long lin[maxn];
  521. inline bool check(int x,long long s){
  522.     return (lin[x]&s)==s;
  523. }
  524. void dfs(int x,long long s){
  525.     if(x==N+1)
  526.         return;
  527.     if(cnt+N-x+1<=ans)
  528.         return;
  529.     if(deg[x]>=ans)
  530.         if(check(x,s)){
  531.             a[++cnt]=x;
  532.             ans=max(ans,cnt);
  533.             dfs(x+1,s|(1LL<<x));
  534.             cnt--;
  535.         }
  536.     dfs(x+1,s);
  537. }
  538. int main(){
  539. //freopen("in","r",stdin);
  540.     while(1){
  541.         N=read();
  542.         if(!N)break;
  543.         for(int i=1;i<=N;i++){
  544.             lin[i]=deg[i]=0;
  545.             for(int j=1;j<=N;j++){
  546.                 e[i][j]=read();
  547.                 if(e[i][j]){
  548.                     deg[i]++;
  549.                     lin[i]|=(1LL<<j);
  550.                 }
  551.             }
  552.         }
  553.         ans=0;
  554.         dfs(1,0);
  555.         printf("%d\n",ans);
  556.     }
  557. return 0;
  558. }
  559. //1008
  560. #include<iostream>
  561. #include<cstdio>
  562. #include<cstring>
  563. #include<ctime>
  564. #include<cstdlib>
  565. #include<algorithm>
  566. #include<cmath>
  567. #include<string>
  568. #include<queue>
  569. #include<vector>
  570. #include<map>
  571. #include<set>
  572. #include<utility>
  573. #include<iomanip>
  574. using namespace std;
  575. int read(){
  576. int xx=0,ff=1;char ch=getchar();
  577. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  578. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  579. return xx*ff;
  580. }
  581. long long READ(){
  582. long long xx=0,ff=1;char ch=getchar();
  583. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  584. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  585. return xx*ff;
  586. }
  587. char one(){
  588.     char ch=getchar();
  589.     while(ch==' '||ch=='\n')
  590.         ch=getchar();
  591.     return ch;
  592. }
  593. const int maxn=110,INF=1<<30;
  594. int N,M,ans,A,B;
  595. char mp[maxn][maxn];
  596. pair<int,int>a[maxn],b[maxn];
  597. int lin[maxn*2],len;
  598. struct edge{
  599.     int y,next,flow,v;
  600. }e[maxn*maxn*2];
  601. inline void insert(int xx,int yy,int ff,int vv){
  602.     e[++len].next=lin[xx];
  603.     lin[xx]=len;
  604.     e[len].y=yy;
  605.     e[len].flow=ff;
  606.     e[len].v=vv;
  607. }
  608. inline void ins(int xx,int yy,int ff,int vv){
  609.     insert(xx,yy,ff,vv);
  610.     insert(yy,xx,0,-vv);
  611.     //printf("%d %d %d %d\n",xx,yy,ff,vv);
  612. }
  613. int dis[maxn*2],pre[maxn*2],useedge[maxn*2],q[maxn*maxn],head,tail,ss,ee;
  614. bool vis[maxn*2];
  615. bool SPFA(){
  616.     memset(vis,0,sizeof(vis));
  617.     memset(dis,10,sizeof(dis));
  618.     head=tail=0;
  619.     q[head]=ss,vis[ss]=1,dis[ss]=0;
  620.     for(;head<=tail;head++){
  621.         vis[q[head]]=0;
  622.         for(int i=lin[q[head]];i;i=e[i].next){
  623.             if(e[i].flow&&(dis[e[i].y]>dis[q[head]]+e[i].v)){
  624.                 dis[e[i].y]=dis[q[head]]+e[i].v;
  625.                 if(!vis[e[i].y]){
  626.                     vis[e[i].y]=1;
  627.                     q[++tail]=e[i].y;
  628.                 }
  629.                 pre[e[i].y]=q[head];
  630.                 useedge[e[i].y]=i;
  631.             }
  632.         }
  633.     }
  634.     return dis[ee]!=dis[0];
  635. }
  636. void agu(){
  637.     int add=INF;
  638.     for(int i=ee;i!=ss;i=pre[i])
  639.         add=min(add,e[useedge[i]].flow);
  640.     for(int i=ee;i!=ss;i=pre[i]){
  641.         e[useedge[i]].flow-=add;
  642.         if(useedge[i]&1)
  643.             e[useedge[i]+1].flow+=add;
  644.         else
  645.             e[useedge[i]-1].flow+=add;
  646.         ans+=add*e[useedge[i]].v;
  647.     }
  648. }
  649. void cost_flow(){
  650.     ans=0;
  651.     while(SPFA())
  652.         agu();
  653. }
  654. int calc(int x,int y){
  655.     return abs(a[x].first-b[y].first)+abs(a[x].second-b[y].second);
  656. }
  657. void build(){
  658.     memset(lin,0,sizeof(lin));len=0;
  659.     ss=A+B+1,ee=A+B+2;
  660.     for(int i=1;i<=A;i++)
  661.         ins(ss,i,1,0);
  662.     for(int i=1;i<=B;i++)
  663.         ins(i+A,ee,1,0);
  664.     for(int i=1;i<=A;i++)
  665.         for(int j=1;j<=B;j++)
  666.             ins(i,j+A,1,calc(i,j));
  667. }
  668. int main(){
  669.     //freopen("in","r",stdin);
  670.     while(1){
  671.         N=read(),M=read();A=B=0;
  672.         if(N+M==0)break;
  673.         for(int i=1;i<=N;i++)
  674.             for(int j=1;j<=M;j++){
  675.                 mp[i][j]=one();
  676.                 if(mp[i][j]=='m')
  677.                     a[++A]=make_pair(i,j);
  678.                 else if(mp[i][j]=='H')
  679.                     b[++B]=make_pair(i,j);
  680.             }
  681.         build();
  682.         cost_flow();
  683.         printf("%d\n",ans);
  684.     }
  685.     return 0;
  686. }
  687. //1009
  688. #include<iostream>
  689. #include<cstdio>
  690. #include<cstring>
  691. #include<ctime>
  692. #include<cstdlib>
  693. #include<algorithm>
  694. #include<cmath>
  695. #include<string>
  696. #include<queue>
  697. #include<vector>
  698. #include<map>
  699. #include<set>
  700. #include<utility>
  701. #include<iomanip>
  702. using namespace std;
  703. int read(){
  704. int xx=0,ff=1;char ch=getchar();
  705. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  706. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  707. return xx*ff;
  708. }
  709. long long READ(){
  710. long long xx=0,ff=1;char ch=getchar();
  711. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  712. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  713. return xx*ff;
  714. }
  715. char one(){
  716.     char ch=getchar();
  717.     while(ch==' '||ch=='\n')
  718.         ch=getchar();
  719.     return ch;
  720. }
  721. const int maxn=8,ws_[4]={1,-1,0,0},ad_[4]={0,0,1,-1};
  722. int N,M,mp[maxn][maxn],ax,ay,bx,by;
  723. struct status{
  724.     int sx,sy,fx,fy,v;
  725.     status(int sx=0,int sy=0,int fx=0,int fy=0,int v=0){
  726.         this->sx=sx,this->sy=sy,this->fx=fx,this->fy=fy,this->v=v;
  727.     }
  728.     bool friend operator<(const status&A,const status&B){
  729.         if(A.sx!=B.sx)return A.sx<B.sx;
  730.         if(A.sy!=B.sy)return A.sy<B.sy;
  731.         if(A.fx!=B.fx)return A.fx<B.fx;
  732.         return A.fy<B.fy;
  733.     }
  734. };
  735. deque<status>q;
  736. map<status,bool>vis;
  737. inline bool out(int x,int y){
  738.     return x<1||y<1||x>N||y>M;
  739. }
  740. int bfs(){
  741.     vis.clear();
  742.     while(!q.empty())q.pop_front();
  743.     status init(ax,ay,bx,by);
  744.     vis[init]=1;
  745.     q.push_front(init);
  746.     while(!q.empty()){
  747.         status t=q.front();
  748.         q.pop_front();
  749.         for(int i=0;i<4;i++){
  750.             status nxt=t;
  751.             nxt.fx=t.fx+ws_[i],nxt.fy=t.fy+ad_[i];
  752.             if(out(nxt.fx,nxt.fy))continue;
  753.             if(mp[nxt.fx][nxt.fy]==1)continue;
  754.             if(nxt.fx==nxt.sx&&nxt.fy==nxt.sy){
  755.                 nxt.sx+=ws_[i],nxt.sy+=ad_[i];
  756.                 if(out(nxt.sx,nxt.sy))continue;
  757.                 if(mp[nxt.sx][nxt.sy]==1)continue;
  758.                 if(!vis[nxt]){
  759.                     nxt.v++;
  760.                     if(mp[nxt.sx][nxt.sy]==3)
  761.                         return nxt.v;
  762.                     vis[nxt]=1;
  763.                     q.push_back(nxt);
  764.                 }
  765.             }
  766.             else{
  767.                 if(!vis[nxt]){
  768.                     vis[nxt]=1;
  769.                     q.push_front(nxt);
  770.                 }
  771.             }
  772.         }
  773.     }
  774.     return -1;
  775. }
  776. int main(){
  777.     //freopen("in","r",stdin);
  778.     for(int _=read();_;_--){
  779.         N=read(),M=read();
  780.         for(int i=1;i<=N;i++)
  781.             for(int j=1;j<=M;j++){
  782.                 mp[i][j]=read();
  783.                 if(mp[i][j]==2)ax=i,ay=j;
  784.                 else if(mp[i][j]==4)bx=i,by=j;
  785.             }
  786.         printf("%d\n",bfs());
  787.     }
  788.     return 0;
  789. }
  790. //1010
  791. #include<iostream>
  792. #include<cstdio>
  793. #include<cstring>
  794. #include<ctime>
  795. #include<cstdlib>
  796. #include<algorithm>
  797. #include<cmath>
  798. #include<string>
  799. #include<queue>
  800. #include<vector>
  801. #include<map>
  802. #include<set>
  803. #include<utility>
  804. #include<iomanip>
  805. using namespace std;
  806. int read(){
  807. int xx=0,ff=1;char ch=getchar();
  808. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  809. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  810. return xx*ff;
  811. }
  812. long long READ(){
  813. long long xx=0,ff=1;char ch=getchar();
  814. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  815. while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
  816. return xx*ff;
  817. }
  818. char one(){
  819.     char ch=getchar();
  820.     while(ch==' '||ch=='\n')
  821.         ch=getchar();
  822.     return ch;
  823. }
  824. const int maxn=8,ws_[4]={1,-1,0,0},ad_[4]={0,0,1,-1};
  825. int N,M,mp[maxn][maxn],ax,ay,bx,by;
  826. struct status{
  827.     int sx,sy,fx,fy,v;
  828.     status(int sx=0,int sy=0,int fx=0,int fy=0,int v=0){
  829.         this->sx=sx,this->sy=sy,this->fx=fx,this->fy=fy,this->v=v;
  830.     }
  831.     bool friend operator<(const status&A,const status&B){
  832.         if(A.sx!=B.sx)return A.sx<B.sx;
  833.         if(A.sy!=B.sy)return A.sy<B.sy;
  834.         if(A.fx!=B.fx)return A.fx<B.fx;
  835.         return A.fy<B.fy;
  836.     }
  837. };
  838. deque<status>q;
  839. map<status,bool>vis;
  840. inline bool out(int x,int y){
  841.     return x<1||y<1||x>N||y>M;
  842. }
  843. int bfs(){
  844.     vis.clear();
  845.     while(!q.empty())q.pop_front();
  846.     status init(ax,ay,bx,by);
  847.     vis[init]=1;
  848.     q.push_front(init);
  849.     while(!q.empty()){
  850.         status t=q.front();
  851.         q.pop_front();
  852.         for(int i=0;i<4;i++){
  853.             status nxt=t;
  854.             nxt.fx=t.fx+ws_[i],nxt.fy=t.fy+ad_[i];
  855.             if(out(nxt.fx,nxt.fy))continue;
  856.             if(mp[nxt.fx][nxt.fy]==1)continue;
  857.             if(nxt.fx==nxt.sx&&nxt.fy==nxt.sy){
  858.                 nxt.sx+=ws_[i],nxt.sy+=ad_[i];
  859.                 if(out(nxt.sx,nxt.sy))continue;
  860.                 if(mp[nxt.sx][nxt.sy]==1)continue;
  861.                 if(!vis[nxt]){
  862.                     nxt.v++;
  863.                     if(mp[nxt.sx][nxt.sy]==3)
  864.                         return nxt.v;
  865.                     vis[nxt]=1;
  866.                     q.push_back(nxt);
  867.                 }
  868.             }
  869.             else{
  870.                 if(!vis[nxt]){
  871.                     vis[nxt]=1;
  872.                     q.push_front(nxt);
  873.                 }
  874.             }
  875.         }
  876.     }
  877.     return -1;
  878. }
  879. int main(){
  880.     //freopen("in","r",stdin);
  881.     for(int _=read();_;_--){
  882.         N=read(),M=read();
  883.         for(int i=1;i<=N;i++)
  884.             for(int j=1;j<=M;j++){
  885.                 mp[i][j]=read();
  886.                 if(mp[i][j]==2)ax=i,ay=j;
  887.                 else if(mp[i][j]==4)bx=i,by=j;
  888.             }
  889.         printf("%d\n",bfs());
  890.     }
  891.     return 0;
  892. }

ECUST_Algorithm_2019_4的更多相关文章

随机推荐

  1. 高手教您编写简单的JSON解析器

    编写JSON解析器是熟悉解析技术的最简单方法之一.格式非常简单.它是递归定义的,所以与解析Brainfuck相比,你会遇到轻微的挑战 ; 你可能已经使用JSON.除了最后一点之外,解析 Scheme的 ...

  2. Spring----事件(Application Event)

    1.概述 1.1.Spring的事件  为Bean与Bean之间的消息通信提供了支持: 当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要   让另一个Bean  ...

  3. CF1111C Creative Snap 线段树

    用线段树模拟一下就好了~ code: #include <cstdio> #include <algorithm> #define lson ls[x] #define rso ...

  4. mobx学习笔记03——mobx基础语法(decorator修饰器)

    在声明阶段实现类与类成员注解的一种语法. function log(target){ const desc = Object.getOwnPropertyDescriotors(target.prot ...

  5. 拦截Restful API的三种方式

    如题, 方式有三种. (1). 过滤器filter javaEE规范 (2). 拦截器interceptor springmvc提供 (3). 切片 aspect 一. Filter使用示例 impo ...

  6. 重写UIlabel的setText:方法,过滤或者拦截text设置

    因为项目中很多地方都有对UIlabel的赋值,但是text.length == 0 或者为空时并没有去给默认值,导致很多界面空间是白板, 所以不想一个一个去改.希望能重写UIlabel 的setTex ...

  7. 在python3.7下怎么安装matplotlib与numpy

    一.安装matplotlib 1.在Matplotlib的官网下载电脑对应的版本,网址为:https://pypi.org/project/matplotlib/#files 2.将在下载的.whl文 ...

  8. BZOJ 3456: 城市规划(dp+多项式求逆)

    传送门 解题思路 这道题就是求带标号的无向连通图个数,首先考虑\(O(n^2)\)的做法,设\(f_i\)表示有\(i\)个节点的无向连通图个数,那么考虑容斥,先把所有的无向图求出,即为\(2^{C( ...

  9. 转载:eclipse中web项目小地球没了

    转载自:{FROM:http://www.cnblogs.com/zhouyalei/archive/2013/01/30/2882651.html} MyEclipse下创建的项目 导入eclips ...

  10. 20175120彭宇辰 《Java程序设计》第十一周学习总结

    教材内容总结 第十三章 Java网络编程 一.URL类 一个URL对象包含的三个基本信息:协议.地址和资源. -协议:必须是URL对象所在的Java虚拟机支持的协议,常用的有:Http.Ftp.Fil ...