程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces Round #254 (Div. 2) A,B

Codeforces Round #254 (Div. 2) A,B

編輯:關於C++
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

DZY loves chessboard, and he enjoys playing with it.

He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.

You task is to find any suitable placement of chessmen on the given chessboard.

Input

The first line contains two space-separated integers n and m (1?≤?n,?m?≤?100).

Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.

Output

Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.

If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.

Sample test(s) input
1 1
.
output
B
input
2 2
..
..
output
BW
WB
input
3 3
.-.
---
--.
output
B-B
---
--B

Note

A題的題意是給你一個矩陣,‘-’代表壞的細胞,‘.’代表好的細胞,而我們需要將好的細胞變化為‘W’或‘B’,且每個相鄰的細胞不能一樣,開始我是直接處理用一個for循環遍歷‘.’的4個方向,如果有’w‘直接跳出標記為’B‘,反之為’W‘,但是這樣的做法是不行的,因為會有一個細胞的相鄰的4個細胞會既有’W‘又有’B‘,所以先將所有的細胞都標記為好的,然後去填充’W‘和’B‘,然後把’-‘的位置標記一下,輸出的時候按照標記數組輸出。以下是代碼。

 

#include
#include
#include
#include
using namespace std;
char s[200][200];
int vx[] = { 0, 0, 1, -1 };
int vy[] = { 1, -1, 0, 0 };
int vis[200][200];
int main()
{
    //freopen("i.txt", "r",stdin);
    //freopen("o.txt", "w",stdout);
    int n, m;
    
    while (~scanf("%d%d", &n, &m))
    {
        memset(s, 0, sizeof(s));
        memset(vis, 0, sizeof(vis));
        int tt;
        for (int i = 0; i < n; i++)
            scanf("%s", s[i]);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; s[i][j]!='\0'; j++)
            {
                if (s[i][j] == '-')
                    vis[i][j] = 1;
                tt = 1;
                {
                    tt = 0;
                    for (int k = 0; k < 4; k++)
                    {
                        int x = i + vx[k];
                        int y = j + vy[k];
                        if (x < 0 || y < 0 || x >= n || y>m)
                            continue;
                        if (s[x][y] == 'B')
                        {
                            tt = 1;
                            break;
                        }
                    }
                }
                if (tt)
                {
                    s[i][j] = 'W';
                }
                else
                    s[i][j] = 'B';
            }
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; s[i][j] != '\0'; j++)
            {
                printf("%c", vis[i][j] ? '-' : s[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

DZY loves chemistry, and he enjoys mixing chemicals.

DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.

Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.

Find the maximum possible danger after pouring all the chemicals one by one in optimal order.

Input

The first line contains two space-separated integers n and m \.

Each of the next m lines contains two space-separated integers xi and yi (1?≤?xi?yi?≤?n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.

Consider all the chemicals numbered from 1 to n in some order.

Output

Print a single integer — the maximum possible danger.

Sample test(s) input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note 這一題是考查並查集的,貌似也可以用深搜.需要注意的是結果會很大,要用long long去定義。
#include
#include
using namespace std;
int a[10000];
int n,m,c,d;
int find(int x)
{
    int r=x;
    while(r!=a[r])
        r=a[r];
    return r;
}
void unio(int x,int y)
{
    int fx,fy;
    fx=find(x);
    fy=find(y);
    if(fx!=fy)
        a[fx]=fy;
}
long long f(int x)
{
    long long ans=1;
    for(int i=1;i<=n-x;i++)
    {
        ans*=2;
    }
    return ans;
}
int main()
{
    
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
            a[i]=i;
        while(m--)
        {
            scanf("%d%d",&c,&d);
            unio(c,d);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==i)
                ans++;
        }
        printf("%lld\n",f(ans));
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved