Digraphs ----DFS
来源:互联网 发布:java解压zip文件 编辑:程序博客网 时间:2024/04/30 02:38
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4601
比赛的时候我并没有做出来这个题,补题的时候我也没有想出来,还是我柏皓巨给我讲的;
题目的意思是说每一行给你两个字符,代表这两个字符不能够相邻,输出尽量大的一个正方形(最大20);
柏皓巨的思路是每行输入的两个字母建一条边,然后遍历他的补图,看有没有成环,如果成环,就循环输出环,否则,则输出最长链的一半,错位输出,因为竖着两个字母也不能够相邻。
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int vis[30][30],cnt,flag,len,v[30];char str[5],ans[30];//这里一定要用char,不能够用stringstring p;void dfs(int x,string s,int fa){ if(v[x]){ if(x==fa){ flag=1; } return ; } v[x]=1; ans[cnt++]=x+'a';//如果ans数组是string类型,则每次cnt++都需要申请空间 if(cnt>len){ len=cnt; p=s+(char)(x+'a'); } for(int i=0;i<26;i++){ if(!vis[x][i]){ dfs(i,s+char(x+'a'),fa); if(flag) return ; } } v[x]=0,cnt--;}int main(){ int t,n,i,j; cin>>t; while(t--){ memset(vis,0,sizeof(vis)); cin>>n; len=flag=cnt=0; while(n--){ cin>>str; vis[str[0]-'a'][str[1]-'a']=1; } for(i=0;i<26&&!flag;i++){ memset(v,0,sizeof(v)); dfs(i,"",i); } if(flag){ for(i=0;i<20;i++){ for(j=0;j<20;j++){ printf("%c",ans[(i+j)%cnt]); } cout<<endl; } } else{ cnt=min((len+1)/2,20); for(i=0;i<cnt;i++){ for(j=0;j<cnt;j++){ printf("%c",p[i+j]); } cout<<endl; } } } return 0;}
0 0
- Digraphs ----DFS
- UVALive 6590 Digraphs (DFS找环找通路)
- UVALive 6590 Digraphs
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- 近日发现一个有趣的数据分析项目 (解析滴滴算法大赛---数据分析过程)
- 从猪和鸡的故事看Scrum Master职责
- Hadoop大数据系列---Hadoop安装与环境配置
- jvm调优
- Nginx中的正则表达式
- Digraphs ----DFS
- php excel导出在本地lamp环境可以运行,阿里云服务器上报错,linux文件夹权限修改
- 使用keepalived实现双机热备
- 12c pdb下的 ABMR测试 Automatic Block Media Recovery by Active Data Guard
- EditText 知识
- Android 「xmlns:tools」「tools:context」ってなんだろう?
- Tensorflow分批量读取数据
- xposed微信模块源代码
- Codeforces 765E - Tree Folding 【树,dfs/bfs】