today

来源:互联网 发布:商品标签制作软件 编辑:程序博客网 时间:2024/06/11 01:56

http://blog.csdn.net/yujuan_mao/article/details/8163415


迷宫问题

#include<iostream>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
using namespace std;
int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
char a[45][45];
struct point
{
int x;
int y;
};
int R,C;
int len;
bool cango(point t)
{
if(t.x>=1 && t.x<=R && t.y>=1 && t.y<=C)
return true;
return false;
}
int visit[45][45]={0};
queue<point>que;
void bfs()
{
point p;
p.x=1;
p.y=1;
que.push(p);
visit[p.x][p.y]=1;
while(!que.empty())
{
point temp=que.front();
que.pop();
point tem;
for(int j=0;j<4;j++)
{
tem.x=temp.x+d[j][0];
tem.y=temp.y+d[j][1];

if(cango(tem) && !visit[tem.x][tem.y] && a[tem.x][tem.y]=='0')
{
visit[tem.x][tem.y]=visit[temp.x][temp.y]+1;
if(tem.x==R && tem.y==C)
{
len=visit[tem.x][tem.y];
cout<<len<<endl;
return;
}
que.push(tem);
}
}
}
}
struct a{
int i;
int j;
}path[6];
void print()
{
int x=5,y=5;
point tp;
tp.x=x;
tp.y=y;
for(int i=len;i>1;i--)
{
path[i].i=tp.x;
path[i].j=tp.y;
for(int t=0;t<4;t++)
{
point tmp;
tmp.x=x+d[t][0];
tmp.y=y+d[t][1];
if(cango(tmp) && visit[tmp.x][tmp.y]==visit[tp.x][tp.y]-1)
{
tp.x=tmp.x;
tp.y=tmp.y;
break;
}
}
}
cout<<"(1,1)";
for(int i=2;i<=len;i++)
cout<<"("<<path[i].i<<","<<path[i].j<<")";
cout<<endl;
}
int main()
{
cin>>R>>C;
for(int i=1;i<=R;i++)
{
for(int j=1;j<=C;j++)
{
cin>>a[i][j];
}
getchar();
}
bfs();
print();
}


找鞍点

#include<iostream>
#include<limits.h>
using namespace std;
int r[6],c[6];
int a[6][6];
int main()
{
for(int i=1;i<=5;i++)
r[i]=INT_MIN;
for(int i=1;i<=5;i++)
c[i]=INT_MAX;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
cin>>a[i][j];
if(a[i][j]>r[i])r[i]=a[i][j];
if(a[i][j]<c[j])c[j]=a[i][j];
}
}
int flag=0;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(r[i]==c[j])
{
cout<<i<<" "<<j<<" "<<r[i]<<endl;
flag=1;
}
}
}
if(flag==0)
cout<<"not found"<<endl;
return 0;
}

单词翻转

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[510];

cin.getline(s,510);
int i=0;
int p;
int j=0;
int len=strlen(s);
while(i<=len){
while(s[i]!=' ' && s[i]!='\0') i++;
p=i+1;


i=i-1;
while(j<i)
{
char c=s[j];
s[j]=s[i];
s[i]=c;
j++;
i--;
}
j=p;
i=p;}
cout<<s<<endl;
return 0;
}

整数奇偶排序

#include<iostream>
#include<algorithm>
using namespace std;
int a[12];
bool cmp(const int&m,const int&n)
{
if(m%2==1 && n%2==0)
return true;
else if(m%2==0 && n%2==1)
return false;
else if(m%2==1 && n%2==1)
return m>n;
else if(m%2==0 && n%2==0)
return m<n;
}
int main()
{
while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]){
sort(a,a+10,cmp);
for(int i=0;i<9;i++)
cout<<a[i]<<" ";
cout<<a[9]<<endl;}
return 0;
}


硬币凑整coin

#include<iostream>
#include<limits.h>
using namespace std;
int count[300020];


int dp[320020];
int num[7],valued[7];
int main()
{

while(1){
int total=0;
int nn,mm;
cin>>nn>>mm;
if(nn==0 && mm==0)return 0;
for(int i=1;i<=nn;i++)
{
cin>>valued[i];
}
for(int i=1;i<=nn;i++)
{
cin>>num[i];
}

int it=1;
for(int i=1;i<=nn;i++)
{
int x=1;int tmp=0;
while(true)
{
if(tmp+x>num[i])break;//zheliyicuo
count[it]=x*valued[i];
tmp+=x;
it++;
x=x*2;
}
x=num[i]-tmp;
if(x>0) 
{
count[it]=x*valued[i];
it++;
}
}
for(int i=1;i<=mm;i++)
dp[i]=0;
dp[0]=0;
for(int i=1;i<it;i++)
{
for(int j=mm;j>=count[i];j--)
{
dp[j]=max(dp[j],dp[j-count[i]]+count[i]);
}
}
int ans=0;
for(int i=1;i<=mm;i++)
{
if(dp[i]==i)
ans++;
}
cout<<ans<<endl;
}
return 0;
}


dividing

#include<iostream>
#include<limits.h>
using namespace std;
int count[300020];


int dp[320020];
int num[7],valued[7];
int main()
{
int time=1;
while(1){
int total=0;
for(int i=1;i<=6;i++)
{
cin>>num[i];
valued[i]=i;
total+=num[i]*valued[i];
}
if(total==0)return 0;
if(total%2!=0)
{cout<<"Collection #"<<time<<":"<<endl;
cout<<"Can't be divided."<<endl<<endl;
time++;
continue;
}
int it=1;
for(int i=1;i<=6;i++)
{
int x=1;int tmp=0;
while(true)
{
if(tmp+x>num[i])break;
count[it]=x*valued[i];
tmp+=x;
it++;
x=x*2;
}
x=num[i]-tmp;
if(x>0) 
{
count[it]=x*valued[i];
it++;
}
}
for(int i=1;i<=total/2;i++)
dp[i]=INT_MIN;
dp[0]=0;
for(int i=1;i<it;i++)
{
for(int j=total/2;j>=count[i];j--)
{
dp[j]=max(dp[j],dp[j-count[i]]+count[i]);
}
}
if(dp[total/2]!=total/2)
{cout<<"Collection #"<<time<<":"<<endl;
cout<<"Can't be divided."<<endl;}
else
{cout<<"Collection #"<<time<<":"<<endl;
cout<<"Can be divided."<<endl;}
cout<<endl;
time++;
}
return 0;
}


我爱北大

#include<iostream>
#include<stack>
#include<map> 
#define INT_MAX 10000000
using namespace std;
map <string,int> m;
map<int,string>rm;


int P;
int path[40][40];
int d[40][40];
void printpath(int p,int q)
{
stack<int>sta;
int temp=q;
while(temp!=p)
{
sta.push(temp);
temp=path[p][temp];
}
sta.push(p);
cout<<rm[sta.top()];
int ki=sta.top();
int ti;
sta.pop();
while(!sta.empty())
{
ti=sta.top();
cout<<"->("<<d[ki][ti]<<")->"<<rm[sta.top()];
ki=sta.top();
sta.pop();
}
cout<<endl;
}
void floyd()
{
for(int i=1;i<=P;i++)
{
for(int j=1;j<=P;j++)
{
if(d[i][j]==INT_MAX)
path[i][j]=-1;
else
path[i][j]=i;
}
}
for(int k=1;k<=P;k++)
{
for(int i=1;i<=P;i++)
{
for(int j=1;j<=P;j++)
{
if(d[i][k]+d[k][j]<d[i][j])
{
d[i][j]=d[i][k]+d[k][j];

path[i][j]=path[k][j];
}
}
}
}
}
int main()
{
cin>>P;
for(int i=1;i<=P;i++)
{
string a;
cin>>a;
m.insert(pair<string,int>(a,i));
rm.insert(pair<int,string>(i,a));
}
int Q;
cin>>Q;
for(int i=1;i<=P;i++)
{
for(int j=1;j<=P;j++)
{
if(i==j)d[i][j]=0;
else d[i][j]=INT_MAX;
}
}
for(int i=1;i<=Q;i++)
{
string b,c;
int e;
cin>>b>>c>>e;
int f=m[b];
int g=m[c];
if(e<d[f][g]&&e<d[g][f])//两个点之间的数据可能有多组,要选取最短的存,否则就WA啦
{
d[f][g]=e;
d[g][f]=e;
}
}
int R;
cin>>R;
floyd();
for(int i=1;i<=R;i++)
{
string h,k;
cin>>h>>k;
int p=m[h];
int q=m[k];
printpath(p,q);
}




0 0
原创粉丝点击