2018 03 18下午

1、 数字(number.cpp) 空间限制:256MB 时间限制:1s Description 给你 n 个 0~9 的数字,请你把它们排成一个不含前导 0 的 n 位数,满足这个数是 233 的倍数。求有多少种可行的方案。 (我们把 n 个数字从 1~n 标号,两个方案不同当且仅当标号序列不同) Input 第一行一个数 n。 第二行 n 个空格隔开的数,为给你的数。保证至少有一个不为 0。 Output一行一个数,表示方案数。 Range 1≤n≤10 Sample number.in 4 2 3 3 0 number.out 2 两种方案分别是:2330 和 2330。(注意两个 3 是视作不同的)

思路:定义2个函数,一个负责判断是否为233的倍数,再排序,取消有前导0的方案

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[100],f[100],n;
int noip(long long x)
{
if(x%233!=0)
return 0;
int count=0;
while(x!=0)
{
count++;
x/=10;
}
if(count<n)
return 0;
else
return 1;
}
int search(int deep,long long number)
{
if(deep==n)
return noip(number);
int ans=0;
for(int i=0;i<n;i++)
{
if(f[i]==1)
continue;
f[i]=1;
ans+=search(deep+1,number*10+num[i]);
f[i]=0;
}
return ans;
}

int main()
{
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
cin>>n;
for(int i=0;i<n;i++)
cin>>num[i];
cout<<search(0,0);
return 0;
}

2、迷宫(maze.cpp) 空间限制:256MB 时间限制:1s Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的 1 表示墙壁,0 表示可以走的路,只能横着走或竖着走, 不能斜着走,要求编程序找出从左上角到右下角的最短路线。 Input 一个 5 × 5 的二维数组,表示一个迷宫。数据保证有唯一解。 Output 步数,以及左上角到右下角的最短路径,格式如样例所示。 Sample Input 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 Sample Output 8 (0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)

思路:DFS水题,直接写标准代码代入,并找到最短路线

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int ac[4]={1,-1,0,0};
int ak[4]={0,0,1,-1};
int ans,n,m,k,gx,gy,a[6][6],noip[6][6];
void dfs(int x,int y)
{
if(x<1||y<1||x>n||y>m||a[x][y]==1)
return ;
if(x==gx&&y==gy)
{
ans++;
return ;
}
for(int i=0;i<4;i++)
{
int a=x+ac[i];
int b=y+ak[i];
if(noip[a][b]==0)
{
noip[a][b]=1;
dfs(a,b);
noip[a][b]=0;
}
}
return ;
}
int main()
{
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
cin>>n>>m>>k;
int sy,sx;
cin>>sx>>sy>>gx>>gy;
for(int i=1;i<=k;i++)
{
int p,q;
cin>>p>>q;
a[p][q]=1;
}
noip[sx][sy]=1;
dfs(sx,sy);
cout<<ans<<endl;
return 0;
}

原文地址:https://www.cnblogs.com/jr-ag/p/8591509.html