问题 A: 胜利大逃亡
时间限制: 2 Sec 内存限制: 33 MB提交: 1 解决: 1
[ 提交][ 状态][ 讨论版]
题目描述
魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
输入
特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
输出
样例输入
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
样例输出
11
提示
/*
这道题是搜索题,广度优先搜索,在搜索的模块会讲
*/
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 55;
int map[N][N][N];
int vis[N][N][N];
int tx[] = {1,-1,0,0,0,0};
int ty[] = {0,0,1,-1,0,0};
int tz[] = {0,0,0,0,1,-1};
int a,b,c,t,ans;
struct Node
{
int x,y,z,step;
};
int abs(int x)
{
return x<0?-x:x;
}
int check(int i,int j,int k)//检查边界
{
if(i<0 || j<0 || k<0 || i>=a || j>=b || k>=c || map[i][j][k])
return 0;
return 1;
}
int bfs(int x,int y,int z)
{
int i;
queue<Node> Q;
Node p,q;
p.x = x;
p.y = y;
p.z = z;
p.step = 0;
vis[x][y][z] = 1;
Q.push(p);
while(!Q.empty())
{
p = Q.front();
Q.pop();
if(p.x == a-1 && p.y == b-1 && p.z==c-1 && p.step<=t)
return p.step;
for(i = 0;i<6;i++)
{
q = p;
q.x+=tx[i];
q.y+=ty[i];
q.z+=tz[i];
if(!vis[q.x][q.y][q.z] && check(q.x,q.y,q.z))
{
q.step++;
vis[q.x][q.y][q.z] = 1;
if(abs(q.x-a+1)+abs(q.y-b+1)+abs(q.z-c+1)+q.step>t)
continue;
Q.push(q);
}
}
}
return -1;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int i,j,k;
scanf("%d%d%d%d",&a,&b,&c,&t);
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for(i = 0;i<a;i++)
for(j = 0;j<b;j++)
for(k = 0;k<c;k++)
scanf("%d",&map[i][j][k]);
ans = bfs(0,0,0);
printf("%d\n",ans);
}
return 0;
}
问题 B: 表达式求值
时间限制: 3 Sec 内存限制: 6 MB提交: 13 解决: 8
[ 提交][ 状态][ 讨论版]
题目描述
实现输入一个表达式求出它的值的计算器,比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)
输入
输出
样例输入
2
1.000+2/4=
((1+2)*5+1)/4=
样例输出
1.50
4.00
提示
/*
这道题是数据结构课本上的改进题
*/
#include <stack>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int priority(char c) {
if(c == '=') return 0;
if(c == '+') return 1;
if(c == '-') return 1;
if(c == '*') return 2;
if(c == '/') return 2;
return 0;
}
void compute(stack<double>& Num,stack<char>& Op) {
double b = Num.top();
Num.pop();
double a = Num.top();
Num.pop();
switch(Op.top()) {
case '+':
Num.push(a+b);
break;
case '-':
Num.push(a-b);
break;
case '*':
Num.push(a*b);
break;
case '/':
Num.push(a/b);
break;
}
Op.pop();
}
int main() {
int z;
char str[1005];
stack<double> Num;
stack<char> Op;
scanf("%d",&z);
while(z--) {
scanf("%s",str);
int len = strlen(str);
for(int i=0; i<len; i++) {
if(isdigit(str[i])) {
double n = atof(&str[i]);
while(i<len && (isdigit(str[i]) || str[i]=='.'))
i++;
i--;
Num.push(n);
} else {
if(str[i] == '(')
Op.push(str[i]);
else if(str[i] == ')') {
while(Op.top()!='(')
compute(Num,Op);
Op.pop();
} else if(Op.empty() || priority(str[i])>priority(Op.top()))
Op.push(str[i]);
else {
while(!Op.empty() && priority(str[i])<=priority(Op.top()))
compute(Num,Op);
Op.push(str[i]);
}
}
}
Op.pop();
printf("%.2f\n",Num.top());
Num.pop();
}
return 0;
}
问题 C: Train
时间限制: 1 Sec 内存限制: 33 MB提交: 12 解决: 2
[ 提交][ 状态][ 讨论版]
题目描述
输入
输出
样例输入
3 123 321
3 123 312
样例输出
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
提示
/*
用栈模拟火车进站的问题
*/
#include<iostream>
#include<stack>
#define max 100
using namespace std;
int main() {
stack<char>s;
int n,i,j,k,result[max];
char str1[max],str2[max];
while(cin>>n>>str1>>str2) {
j=0,i=0,k=1;
s.push(str1[0]);
result[0]=1;
while(i<n&&j<n) { //没有判断到字符串尾
if(s.size()&&s.top()==str2[j]) {
j++;
s.pop();
result[k++]=0;
} else {
if(i==n)
break;
s.push(str1[++i]);
result[k++]=1;
}
}
if(i==n) {
cout<<"No."<<endl;
} else {
cout<<"Yes."<<endl;
for(i=0; i<k; i++)
if(result[i])
cout<<"in"<<endl;
else
cout<<"out"<<endl;
}
cout<<"FINISH"<<endl;
}
return 0;
}
问题 D: 情报分析
时间限制: 1 Sec 内存限制: 12 MB提交: 9 解决: 6
[ 提交][ 状态][ 讨论版]
题目描述
“八一三”淞沪抗战爆发后,*几次准备去上海前线视察和指挥作战。但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行。 特科组织,其主要任务是保卫的安全,了解和掌握敌方的动向。经过一段时间的监听,谍报组获取了敌方若干份密报,经过分析,发现密文中频繁出现一些单词,情报人员试图从单词出现的次数中,推出敌军的行动计划。 请你编程,快速统计出频率高的前十个单词。
输入
输出
样例输入
shooting is at shanghai station. shooting must
be carried out. shooting shooting.
shanghai station must be surrounded, at least a team of one hundred soldiers to fight. twenty five soldiers shooting in the north, twenty five soldiers shooting in the south, twenty five soldiers shooting in the east, twenty five soldiers shooting in the west.
样例输出
shooting 8
soldiers 5
five 4
in 4
the 4
twenty 4
at 2
be 2
must 2
shanghai 2
提示
#include<iostream>
#include<bits/stdc++.h>//万能头文件,包含许多头文件,但是会影响程序
using namespace std;
typedef pair<string, int> PAIR;
struct CmpByValue{
bool operator()(const PAIR& leftValue, const PAIR& rightValue){
return leftValue.second > rightValue.second? 1:(leftValue.second != rightValue.second)?0:leftValue.first<=rightValue.first;
}
};
map<string ,int> mapWord;
int main(){
string str;
while(cin>>str){
if(str[str.length()-1] == ',' || str[str.length()-1] == '.')
str = str.substr(0,str.length()-1);
mapWord[str]++;
}
vector<PAIR> vec(mapWord.begin(), mapWord.end());
sort(vec.begin(), vec.end() ,CmpByValue());
int i = 0;
for(auto it=vec.begin(); it!=vec.end(); ++it,++i){
if(i > 9)break;
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
问题 E: Information Sharing
时间限制: 3 Sec 内存限制: 65 MB提交: 6 解决: 5
[ 提交][ 状态][ 讨论版]
题目描述
There is going to be a test in the kindergarten. Since the kids would cry if they get a low score in the test, the teacher has already told every kid some information about the test in advance. But the kids are not satisfied with the information teacher gave. They want to get more. On the testing day, some kids arrived to the classroom early enough, and then shared his/her information with another. kids are honest, if A shares with B, B can get all the information A knows, so does A. At first the classroom is empty. As time pass by, a kid would arrive, or share information with other. However, the teacher hides somewhere, watching everything. She wants to know how much information some kid has gotten.
输入
输出
样例输入
8
arrive FatSheep 3 4 7 5
arrive riversouther 2 4 1
share FatSheep riversouther
check FatSheep
arrive delta 2 10 4
check delta
share delta FatSheep
check riversouther
样例输出
4
2
5
提示
check 1: FatSheep has 1 4 5 7, having all the information. So answer is 4.
check 2: delta has only 4 10 , doesn't have 1 5 7. So answer is 2
check 3: riversouther has 1 4 5 7 10, having all the information. So answer is 5
/*
并查集与set
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
int cnt;
map<string,int> mp;
const int maxn=100005;
int father[maxn];
set<int> s[maxn];
void init(int i) {
father[i]=i;
}
int find(int p) {
return p==father[p]?p:father[p]=find(father[p]);
}
void unio(int a,int b) {
int fa=find(a),fb=find(b);
if(fa!=fb) {
father[fa]=fb;
for(set<int>::iterator it=s[fa].begin(); it!=s[fa].end(); ++it) {
s[fb].insert(*it);
}
s[fa].clear();
}
}
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
char a[20],b[20];
mp.clear();
cnt=0;
while(n--) {
scanf("%s",a);
if(a[0]=='a') {
scanf("%s",b);
mp[string(b)]=++cnt;
init(cnt);
s[cnt].clear();
int m;
scanf("%d",&m);
for(int i=0; i<m; ++i) {
int x;
scanf("%d",&x);
s[cnt].insert(x);
}
} else if(a[0]=='s') {
char c[20];
scanf("%s%s",b,c);
int x=mp[string(b)],y=mp[string(c)];
unio(x,y);
} else {
scanf("%s",b);
int f=find(mp[string(b)]);
printf("%d\n",s[f].size());
}
}
}
return 0;
}
问题 G: 懒省事的小名
时间限制: 1 Sec 内存限制: 12 MB提交: 122 解决: 45
[ 提交][ 状态][ 讨论版]
题目描述
小名总结了一个英语近义词的词典(很厚哦,有多厚,你猜),词典实在太厚了,你需要帮助他写一个程序,输入一个单词,输出它的近义词
输入
第一行输入近义词的个数N和小名要查询的行数M
接下来N行为近义词对
M行单词,根据单词,输出它的近义词
输出
样例输入
2 2
abc cba
aaa bbb
abc
aaa
样例输出
cba
bbb
提示
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main() {
char s1[2000],s2[2000];
string str1,str2;
map<string, string> map_key;
map<string, string> map_value;
int n,m;
scanf("%d %d",&n,&m);
for(int i=0; i<n; ++i) {
scanf("%s %s",s1,s2);
str1 = s1;
str2 = s2;
map_key[str1] = str2;
map_value[str2] = str1;
}
vector<string> vec;
for(int i=0; i<m; ++i) {
scanf("%s",s1);
str1 = s1;
vec.push_back(str1);
}
for(auto it=vec.begin(); it!=vec.end(); it++) {
int mark = 0;
if(map_key.find((*it)) != map_key.end()) {
cout<<map_key[(*it)]<<endl;
continue;
} else {
mark = 1;
}
if(map_value.find((*it)) != map_value.end()) {
cout<<map_value[(*it)]<<endl;
continue;
} else {
mark = 1;
}
if(mark)cout<<"Null\n";//测试数据中没有匹配失败的情况,太水
}
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct zidian{
string a1;
string a2;
}b[1000];
int main()
{
int N,M;
string str;
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++)
cin>>b[i].a1>>b[i].a2;
for(int j=0;j<M;j++)
{
cin>>str;
for(int k=0;k<N;k++)
{
if(str==b[k].a1)
cout<<b[k].a2<<endl;
if(str==b[k].a2)
cout<<b[k].a1<<endl;
}
}
return 0;
}