3月18日练习记录

STL练习

Where is the Marble?

Title

Raju and Meena love to play with Marbles. They have got a lot of
marbles with numbers written on them. At the beginning, Raju would
place the marbles one after another in ascending order of the numbers
written on them. Then Meena would ask Raju to find the first marble
with a certain number. She would count 1…2…3. Raju gets one point
for correct answer, and Meena gets the point if Raju fails. After some
fixed number of trials the game ends and the player with maximum
points wins. Today it’s your chance to play as Raju. Being the smart
kid, you’d be taking the favor of a computer. But don’t underestimate
Meena, she had written a program to keep track how much time you’re
taking to give all the answers. So now you have to write a program,
which will help you in your role as Raju.

Input

There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins
with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next
N lines would contain the numbers written on the N marbles. These marble numbers will not come
in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers
are greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 and Q = 0.

Output

For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether
or not the query number is written upon any of the marbles. The two different formats are described
below:
• ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered
1, 2, . . . , N.
• ‘x not found’, if the marble with number x is not present.
Look at the output for sample input for details.

Answer

#include<iostream>
#include<algorithm>
using namespace std;
int a[10010];
int n,q;
int main(){
    int cas = 1;
    while(cin >> n >> q ){
       if(n==0 && q==0) break;
        printf("CASE# %d:\n",cas++);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        for(int i=1;i<=q;i++){
            int tem;
            scanf("%d",&tem);
            int pos = lower_bound(a+1, a+1+n, tem) - a;
            if(a[pos] == tem) printf("%d found at %d\n",tem,pos);
            else printf("%d not found\n",tem);
        }
    }
}

Hint

简单stl

DFS

Where is the Marble?

Title

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.
GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides
the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to
determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the
same oil deposit. Oil deposits can be quite large and may contain

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number
of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input;
otherwise 1 ≤ m ≤ 100 and 1 ≤ n ≤ 100. Following this are m lines of n characters each (not counting
the end-of-line characters). Each character corresponds to one plot, and is either ‘*’, representing the
absence of oil, or ‘@’, representing an oil pocket.

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same
oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain
more than 100 pockets.

Answer

#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int n,m;
int idx[N][N];
char mp[N][N];
int cnt;
void dfs(int i,int j,int id){
    if(i<0 || i>=n || j<0 || j>=m) return;
    if(idx[i][j] > 0 || mp[i][j]!='@') return;
    idx[i][j] = id;
    for(int di = -1;di<=1;di++){
        for(int dj=-1;dj<=1;dj++){
            if(di!=0 || dj!=0) dfs(i+di,j+dj,id);
        }
    }
}
int main(){
    while(cin >> n >> m){
        cnt = 0;
        memset(idx,0,sizeof idx);
        if(n==0 && m==0)break;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin >> mp[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(!idx[i][j] && mp[i][j]=='@'){
                    dfs(i,j,++cnt);
                }
            }
        }
        cout << cnt << endl;
    }
}

Hint

从一个点开始DFS,直到遍历失败,再选择没有遍历过的点。

DFS

Where is the Marble?

Title

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.
GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides
the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to
determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the
same oil deposit. Oil deposits can be quite large and may contain

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number
of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input;
otherwise 1 ≤ m ≤ 100 and 1 ≤ n ≤ 100. Following this are m lines of n characters each (not counting
the end-of-line characters). Each character corresponds to one plot, and is either ‘*’, representing the
absence of oil, or ‘@’, representing an oil pocket.

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same
oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain
more than 100 pockets.

Answer

#include<iostream>
#include<cstring>
using namespace std;
const int N = 110;
int n,m;
int idx[N][N];
char mp[N][N];
int cnt;
void dfs(int i,int j,int id){
    if(i<0 || i>=n || j<0 || j>=m) return;
    if(idx[i][j] > 0 || mp[i][j]!='@') return;
    idx[i][j] = id;
    for(int di = -1;di<=1;di++){
        for(int dj=-1;dj<=1;dj++){
            if(di!=0 || dj!=0) dfs(i+di,j+dj,id);
        }
    }
}
int main(){
    while(cin >> n >> m){
        cnt = 0;
        memset(idx,0,sizeof idx);
        if(n==0 && m==0)break;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin >> mp[i][j];
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(!idx[i][j] && mp[i][j]=='@'){
                    dfs(i,j,++cnt);
                }
            }
        }
        cout << cnt << endl;
    }
}

Hint

从一个点开始DFS,直到遍历失败,再选择没有遍历过的点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值