搜索-深搜dfs
来源:互联网 发布:java 反射获取method 编辑:程序博客网 时间:2024/06/04 19:31
//poj1979//dfs 连通路#include<iostream>#include<cstdio>#include<cstring> using namespace std;char shz[25][25];int shz1[25][25];int cnt;int a,b;int x[]={0,0,-1,1};int y[]={1,-1,0,0};void dfs(int ax,int ay){ for(int i=0;i<4;i++){ int x1=ax+x[i]; int y1=ay+y[i]; if(x1>=0&&x1<a&&y1>=0&&y1<b&&shz[x1][y1]!='#'&&shz1[x1][y1]==0){ cnt++; shz1[x1][y1]=1; dfs(x1,y1); } } }void dis(){ for(int i=0;i<a;i++){ for(int j=0;j<b;j++){ cout<<shz1[i][j]; } cout<<endl; }} int main(){ while(cin>>b>>a&&a){ int ax,ay; cnt=1; memset(shz1,0,sizeof(shz1)); for(int i=0;i<a;i++){ for(int j=0;j<b;j++){ cin>>shz[i][j]; if(shz[i][j]=='@'){ ax=i; ay=j; } } } shz1[ax][ay]=1; dfs(ax,ay);// dis(); cout<<cnt<<endl; }} //poj2718#include<iostream>#include<cstdio>#include<cmath>#include<cctype>#include<cstring>#include<map>#include<set>#include<vector>#include<algorithm>#include<queue>using namespace std;int shz[15];int bol[15];int shz1[15];int bol1[15];int flr;int cnt;int gol;int main(){ string s; int T; cin>>T; getchar(); while(T--){ memset(bol,0,sizeof(bol)); gol=1000000; getline(cin,s); cnt=0; for(int i=0;i<s.size();i++){ if(isdigit(s[i])) shz[cnt++]=s[i]-'0'; } if(cnt%2==1){ int len=(cnt+1)/2; int ans=0; for(int i=0;i<len;i++) ans=ans*10+shz[i]; if(shz[0]==0){ ans=shz[1]*10; for(int i=2;i<len;i++) ans=ans*10+shz[i]; } int ans1=0; for(int i=cnt-1;i>=len;i--) ans1=ans1*10+shz[i]; cout<<ans-ans1<<endl; continue; } if(cnt==2&&shz[0]==0){ cout<<shz[1]<<endl; continue; } int head=0; int gol=20000; if(shz[0]==0)head=1; for(int i=head;i<cnt;i++){ bol[i]=1; int ans1=shz[i]; int ans; for(int j=i+1;j<cnt;j++){ if(!bol[j]){ int ans2=shz[j]; bol[j]=1; int len=1,mid=cnt/2,k; for(k=0;k<cnt;k++){ if(len==mid)break; if(!bol[k]){ len++; ans2=ans2*10+shz[k]; } } int m,l=1; for(m=cnt-1;m>=k;m--){ if(l==mid)break; if(!bol[m]){ l++; ans1=ans1*10+shz[m]; } } gol=min(gol,abs(ans1-ans2)); bol[j]=0; } } bol[i]=0; } cout<<gol<<endl; } return 0;}
0 0
- 搜索-深搜dfs
- 搜索 dfs
- dfs搜索
- DFS搜索
- 搜索dfs
- hdu1241Oil Deposits(搜索--深搜DFS)
- hdu 1241 简单 搜索DFS (深搜)
- 深度优先搜索 DFS
- 深度优先搜索 DFS
- DFS搜索讲解
- Frenemies题解搜索DFS
- DFS 深度优先搜索
- [搜索]DFS+BFS
- poj1088 DFS深度搜索
- poj1088滑雪搜索dfs
- 二分搜索+DFS
- HDu1241 DFS搜索
- DFS 深度优先搜索
- 阿里云 ECS(centos 7.2镜像) 搭建 Etherpad(在线协作编辑 与一起写相仿)
- 机器学习基本概念(一)
- Ubuntu Server 16.04.1 安装配置以及简单使用MySQL(云服务器配置)
- List转为map
- 第七章 复用类
- 搜索-深搜dfs
- 机器学习基本概念(二)
- 测试下
- # java AQS设计和实现
- C++异常规范
- 机器学习基本概念(三)
- 智能手机双摄像头工作原理详解:RBG +RGB, RGB + Mono
- 53. Maximum Subarray
- InnoDB存储引擎(一)