双周赛2
输出全排列
签到题,不解释
1 #include <bits/stdc++.h>
2 using namespace std;
3 const int N=15;
4 int path[N];
5 int n;
6 bool st[N];
7 void dfs(int u)
8 {
9 if(u>n)
10 {
11 for(int i=1;i<=n;i++)
12 {
13 cout<<path[i];
14 }
15 cout<<endl;
16 }
17 for(int i=1;i<=n;i++)
18 {
19 if(!st[i])
20 {
21 st[i]=1;
22 path[u]=i;
23 dfs(u+1);
24 st[i]=0;
25 }
26 }
27
28 }
29 int main()
30 {
31 cin>>n;
32 dfs(1);
33 return 0;
34 }
山
连通块染色法,和洛谷扫雷那道题比较相似,扫雷链接:P4961 小埋与扫雷 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
1 #include <bits/stdc++.h>
2 using namespace std;
3 int n,m;
4 const int N=2010;
5 int p[N][N];
6 int ans;
7 bool st[N][N];
8 int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
9 void dfs(int x,int y)
10 {
11 if(p[x][y]==1)
12 {
13 if(st[x][y]==0&&(st[x+dx[0]][y+dy[0]]==0&&st[x+dx[1]][y+dy[1]]==0&&st[x+dx[2]][y+dy[2]]==0&&st[x+dx[3]][y+dy[3]]==0))ans++;
14 st[x][y]=1;
15 for(int i=0;i<4;i++)
16 {
17 int xx=x+dx[i],yy=y+dy[i];
18 if(!st[xx][yy]&&p[xx][yy]==1&&xx>=1&&xx<=m&&yy>=1&&yy<=n)
19 {
20 dfs(xx,yy);
21 }
22 }
23 }
24 }
25 int main()
26 {
27 cin>>m>>n;
28 for(int i=1;i<=m;i++)
29 for(int j=1;j<=n;j++)
30 cin>>p[i][j];
31 for(int i=1;i<=m;i++)
32 for(int j=1;j<=n;j++)
33 dfs(i,j);//染色
34 cout<<ans;
35 }
跳跃
题目链接:题目详情 - 7-3 跳跃 (pintia.cn)
因为要是找到直接输出True,100次经过某个坐标还没有找到那说明陷入了某种死循环,肯定没有找到,此时输出False
1 #include <bits/stdc++.h>
2 using namespace std;
3 int n,st;
4 const int N=1e5+10;
5 int a[N];
6 int cnt[N];
7 bool flag=0;
8 bool flag2=0;
9 void dfs(int x)
10 {
11 if(flag==1)return;
12 if(flag2==1)return;
13 cnt[x]++;
14 if(cnt[x]==100)
15 {
16 cout<<"False";
17 flag2=1;
18 return;
19 }
20 if(a[x]==0)
21 {
22 cout<<"True";
23 flag=1;
24 return;
25 }
26 for(int i=0;i<n;i++)
27 {
28 int up=x+a[x],down=x-a[x];
29 if(up>=0&&up<n)dfs(up);
30 if(down>=0&&down<n)dfs(down);
31 }
32 }
33 int main()
34 {
35 cin>>n;
36 for(int i=0;i<n;i++)cin>>a[i];
37 cin>>st;
38 dfs(st);
39 return 0;
40 }
回文数文回
既用到了string s(str, stridx, strlen) ; // 将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值
也用到了C++之to_string。 功能:将数字常量转化为字符串 返回值:转换好的字符串

1 #include <iostream> // std::cout
2 #include <string> // std::string, std::to_string
3 using namespace std ;
4
5 int main()
6 {
7 std::string pi = "pi is " + std::to_string(3.1415926);
8 std::string perfect = std::to_string(1 + 2 + 4 + 7 + 14) + " thshis a perfect number";
9 std::cout << pi << '\n';
10 std::cout << perfect << '\n';
11
12 system("pause");
13
14
15 }
输出:
我们可以采用构造回文数的方法,且很容易知道题中回文数的数量最多就9*10*10*10*10个,因为选定前五个就可以确定后面的四个了,简而言之就是每1w至少有一个回文数,但是还是需要注意一点是有可能还未满1w,但是例如123454321和123454322一样他们就不止有12345个回文,而是有12345+1个
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main() {
4 int a, ans = 0;
5 cin >> a;
6 string check = to_string(a);
7 string A(check, 0, 4), B(check, 5, 8);
8 reverse(A.begin(), A.end());
9 if (A <= B)ans++;
10 ans += a / 10000 - 10000;
11 cout << ans;
12 return 0;
13 }
最长光路
这个主要难点是如何确定回环,单纯用二维坐标是不行的,所以我们可以尝试一下再加一维用来判断方向用vis[x][y][dir]就行了
1 /*思路:有一个check,一个vis[x][y][to],再dfs且不需要回溯,如果形成回环则相当于下一个是被标记的1*/
2 #include<bits/stdc++.h>
3 using namespace std;
4 int N, M, sx, sy, len, ans; char dirction;
5 const int mx = 0x3ffffff;
6 char mp[505][505];//也可以用一个string来储存
7 char to[4] = {'U','R','D','L'};
8 pair<int, int >way[4] = { {-1,0},{0,1},{1,0},{0,-1} };//这个地方很细节注意这个坐标系是以下方为x轴正方向,右方向为y轴正方向
9 bool vis[505][505][4];//前俩个表示x,y坐标,最后一个用来表示方向
10 bool check(int x, int y) {
11 if (x<1 || y<1 || x>N || y>M || mp[x][y] == 'C')return false;
12 else return true;
13 }
14 void dfs(int x, int y, int dir) {
15 len++;
16 if (vis[x][y][dir]) {//表明形成了环状则
17 len = mx;
18 return;
19 }
20 else vis[x][y][dir] = true;//标记为走过
21 if (mp[x][y] == '\\') {
22 if (to[dir] == 'U')dir = 3;
23 else if (to[dir] == 'R')dir = 2;
24 else if (to[dir] == 'D')dir = 1;
25 else if (to[dir] == 'L')dir = 0;
26 }
27 if (mp[x][y] == '/') {
28 if (to[dir] == 'U')dir = 1;
29 else if (to[dir] == 'R')dir = 0;
30 else if (to[dir] == 'D')dir = 3;
31 else if (to[dir] == 'L')dir = 2;
32 }
33 if (!check(x + way[dir].first, y + way[dir].second))return;
34 dfs(x + way[dir].first, y + way[dir].second, dir);
35
36 }
37 int main(){
38 cin >> N >> M;
39 for (int i = 1; i <= N; i++) {
40 for (int j = 1; j <= M; j++) {
41 cin >> mp[i][j];
42 }
43 }
44 cin >> sx >> sy;
45 for (int i = 0; i <= 3; i++) {
46 memset(vis, 0, sizeof(vis));
47 len = 0;
48 dfs(sx, sy, i);
49 if (len > ans) {
50 dirction = to[i];
51 ans = len;
52 }
53 }
54 cout << dirction << endl;
55 if (ans == mx)cout << "COOL" << endl;
56 else cout << ans << endl;
57 return 0;
58 }
本文作者:Zac-saodiseng
本文链接:https://www.cnblogs.com/Zac-saodiseng/p/16953463.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步