第五次寒假集訓

要求(A/B)%9973,但由于A很大,我們只給出n(n=A%9973)(我們給定的A必能被B整除笑跛,且gcd(B,9973) = 1)熬甚。
Input
數(shù)據(jù)的第一行是一個T逢渔,表示有T組數(shù)據(jù)。
每組數(shù)據(jù)有兩個數(shù)n(0 <= n < 9973)和B(1 <= B <= 10^9)乡括。
Output
對應(yīng)每組數(shù)據(jù)輸出(A/B)%9973肃廓。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060

百度到的模運算的性質(zhì):

                   (a+b) % c==(a % c + b % c) %c ,

                   (a-b) % c==(a % c - b % c) % c,

                    (a*b) % c==(a % c * b % c) % c。

分析:
能夠運用歐幾里德算法解出gcd(a,b)=ax1+by1中的x1诲泌、y1的值

n=A%9973盲赊,則n=A-A/99739973。
又A/B=x敷扫。則A=Bx哀蘑。
所以Bx-A/9973
9973=n。即Bx-9973y=n葵第。
到這里我們能夠發(fā)現(xiàn):僅僅要求出x的值绘迁,就可以算出x%9973。也就是(A/B)%9973了卒密。

#include<iostream>
using namespace std;

int main()
{
    int i, b, n, t;
    cin>>t;
    while (t--)
    {
        cin>> n>> b;
        for (i = 0;i < 9973;i++)
            if ((((b % 9973)*i) % 9973 - n) % 9973 == 0)
            {
                break;
            }
        cout<<i;
    }
    return 0;
}

定義一個二維數(shù)組:


image.png

它表示一個迷宮缀台,其中的1表示墻壁,0表示可以走的路哮奇,只能橫著走或豎著走膛腐,不能斜著走,要求編程序找出從左上角到右下角的最短路線鼎俘。
Input
一個5 × 5的二維數(shù)組哲身,表示一個迷宮。數(shù)據(jù)保證有唯一解贸伐。
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
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

一個5 × 5的二維數(shù)組,用BFS误辑。從左上角(0, 0)位置開始沧踏,上下左右進行搜索,可以定義一個方向數(shù)組巾钉,代表上下左右四個方向翘狱,使用方向數(shù)組,可以使一個點上下左右移動砰苍。用來表示從左上角到右下角的最短路徑中每個元素的前一個元素的下標潦匈,即保存路徑,方便后面的輸出赚导。
通過廣度搜索借助隊列進行操作茬缩,我們可以把已走過的路標記為1,這樣能保證路徑最短吼旧,直到搜索到右下角(4, 4)凰锡,輸出路徑即可。

#include <queue>
#include <iostream>
using namespace std;

int maze[7][7], pre[30], vis[30];
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };

bool isLeagal(int r, int c)
{
    if (r < 0 || c < 0 || r > 4 || c > 4)
        return false;
    if (maze[r][c] == 1)
        return false;
    return true;
}

void print(int des)
{
    if (pre[des] != -1)
        print(pre[des]);
    cout << '(' << des / 5 << ", " << des % 5 << ')' << endl;
}

void bfs()
{
    queue<int> que;
    memset(vis, 0, sizeof(vis));

    pre[0] = -1;
    vis[0] = true;
    que.push(0);
    int now, next;
    int r, c, tr, tc;
    while (!que.empty())
    {
        now = que.front();
        que.pop();
        r = now / 5;
        c = now % 5;

        for (int i = 0; i < 4; i++)
        {
            tr = r + dir[i][0];
            tc = c + dir[i][1];
            next = tr * 5 + tc;
            if (isLeagal(tr, tc) && !vis[next])
            {
                pre[next] = now;
                if (next == 24) return;
                vis[next] = true;
                que.push(next);
            }
        }
    }
}

int main()
{
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            cin >> maze[i][j];
    bfs();
    print(24);
    return 0;
}

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
image.png

Input
The input consists of several test cases. The first line of each case contains two integers n (1 ≤ n ≤ 1000)
and d, where n is the number of islands in the sea and d is the distance of coverage of the radar
installation. This is followed by n lines each containing two integers representing the coordinate of the
position of each island. Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros.
Output
For each test case output one line consisting of the test case number followed by the minimal number
of radar installations needed. ‘-1’ installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
輸入島嶼的數(shù)量坐標還有雷達的探測范圍圈暗,求至少需要多少個雷達掂为。
如果覆蓋不到,就輸出-1.用貪心算法求最優(yōu)解员串。
將島嶼的坐標存儲在結(jié)構(gòu)體中勇哗,將島嶼按距離順序排列,一個一個取出寸齐,是否能同時存在一個圓中欲诺,如果不能則需要一個新的雷達......

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int x;
    int y;
}land[1001];
bool cmp(node a, node b)
{
    if (a.x != b.x)
        return a.x < b.x;
    return a.y > b.y;
}

int main()
{
    ios::sync_with_stdio(false);
    int n;
    int maxy;
    long long d;
    int cas = 1;
    while (cin >> n >> d)
    {
        maxy = 0;
        if (n == 0 && d == 0)
            break;
        bool suc = true;
        for (int i = 0;i < n;i++)
        {
            cin >> land[i].x >> land[i].y;
            maxy = max(maxy, land[i].y);  
        }
        sort(land, land + n, cmp);
        int cnt = 1;
        int i = 1;
        if (maxy > d)
        {
            cout << "Case " << cas++ << ": " << -1 << endl; 
            continue;
        }
        double r = land[0].x + sqrt(d*d - land[0].y*land[0].y);
        while (i < n)
        {
            double rr = land[i].x + sqrt(d*d - land[i].y*land[i].y);
            if (rr < r)
            {
                r = rr;
                i++;
                continue;
            }
            if (sqrt((land[i].x - r)*(land[i].x - r) + land[i].y*land[i].y) > d)
            {
                cnt++;
                r = rr;
            }
            i++;
        }
        cout << "Case " << cas++ << ": " << cnt << endl;
    }
    return 0;
}

Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
? #, a wall
? ., a passable square
? J, Joe’s initial position in the maze, which is a passable square
? F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.


image.png

Joe被困在著火的迷宮里,火勢隨著時間蔓延渺鹦,問Joe是否能夠安全到達迷宮邊界
坑點:可以有多個起火點扰法,Joe只有一個出發(fā)點。
用兩遍BFS海铆,一次記錄每個時間的火勢迹恐,一次搜索Joe的可行路線挣惰。還沒完全弄懂卧斟,跟不上大佬們的操作。

#include <iostream>
#include <queue>
#include <memory.h>
#include <stdio.h>
#define MAX 1100
using namespace std;
struct P//用來存儲路徑的節(jié)點
{
    int r, c;
    int step;
    P(int _r, int _c, int _s)
    {
        r = _r, c = _c, step = _s;
    }
    P() {}
};

const int dr[] = { 0,0,1,-1 };
const int dc[] = { 1,-1,0,0 };
char Map[MAX][MAX];//存儲地圖
int visit[MAX][MAX];//BFS中的visit數(shù)組
int times[MAX][MAX];//記錄每個地方被燒到所需的時間
int r, c;//行憎茂,列
int Step;//記錄最終逃脫所需的步數(shù)或時間
P J;
queue<P> q;

void clear_q()//清除隊列中的數(shù)據(jù)
{
    while (!q.empty())
        q.pop();
}

void show_time()
{
    for (int i = 0;i < r;i++)
    {
        for (int j = 0;j < c;j++)
        {
            cout << times[i][j] << ' ';
        }
        cout << endl;
    }
}

void bfs_fire()
{
    int i;
    int R, C, Time;
    while (!q.empty())
    {
        P temp = q.front();
        q.pop();
        for (i = 0;i < 4;i++)
        {
            R = temp.r + dr[i];
            C = temp.c + dc[i];
            Time = temp.step + 1;
            if (R >= 0 && R < r&&C >= 0 && C < c && (Map[R][C] == '.' || Map[R][C] == 'J') && visit[R][C] == 0)
            {
                visit[R][C] = 1;
                q.push(P(R, C, Time));
                times[R][C] = Time;
            }
        }
    }
}


int bfs_joe()
{
    int i;
    int R, C, Time;
    clear_q();
    q.push(J);
    visit[J.r][J.c] = 1;
    while (!q.empty())
    {
        P temp = q.front();
        q.pop();
        if (temp.r == 0 || temp.r == (r - 1) || temp.c == 0 || temp.c == (c - 1))
        {
            Step = temp.step + 1;
            return 1;
        }
        for (i = 0;i < 4;i++)
        {
            R = temp.r + dr[i];
            C = temp.c + dc[i];
            Time = temp.step + 1;
            if (R >= 0 && R < r&&C >= 0 && C < c&&Map[R][C] == '.'&&visit[R][C] == 0 && Time < times[R][C])
            {
                visit[R][C] = 1;
                q.push(P(R, C, Time));
            }
        }
    }
    return 0;
}

int main()
{
    int i, j;
    int t;
    cin >> t;
    while (t--)
    {
        clear_q();
        cin >> r >> c;
        for (i = 0;i < r;i++)
        {
            cin >> Map[i];
            for (j = 0;j < c;j++)
            {
                if (Map[i][j] == 'F')
                {
                    q.push(P(i, j, 0));
                    times[i][j] = 0;
                    visit[i][j] = 1;
                }
                if (Map[i][j] == 'J')
                {
                    J.r = i;
                    J.c = j;
                    J.step = 0;
                }
            }
        }

        for (i = 0;i < r;i++)
        {
            for (j = 0;j < c;j++)
            {
                times[i][j] = 1000000007;
            }
        }
        memset(visit, 0, sizeof(visit));
        bfs_fire();
        memset(visit, 0, sizeof(visit));
        if (bfs_joe())
            cout << Step << endl;
        else
            cout << "IMPOSSIBLE" << endl;

    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末珍语,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子竖幔,更是在濱河造成了極大的恐慌板乙,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異募逞,居然都是意外死亡蛋铆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門放接,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刺啦,“玉大人,你說我怎么就攤上這事纠脾÷耆常” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵苟蹈,是天一觀的道長糊渊。 經(jīng)常有香客問我,道長慧脱,這世上最難降的妖魔是什么渺绒? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮菱鸥,結(jié)果婚禮上芒篷,老公的妹妹穿的比我還像新娘。我一直安慰自己采缚,他們只是感情好针炉,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扳抽,像睡著了一般篡帕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贸呢,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天镰烧,我揣著相機與錄音,去河邊找鬼楞陷。 笑死怔鳖,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的固蛾。 我是一名探鬼主播结执,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼艾凯!你這毒婦竟也來了献幔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤趾诗,失蹤者是張志新(化名)和其女友劉穎蜡感,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡郑兴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年犀斋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片情连。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡闪水,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蒙具,到底是詐尸還是另有隱情球榆,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布禁筏,位于F島的核電站持钉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏篱昔。R本人自食惡果不足惜每强,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望州刽。 院中可真熱鬧空执,春花似錦、人聲如沸穗椅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匹表。三九已至门坷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間袍镀,已是汗流浹背默蚌。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留苇羡,地道東北人绸吸。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像设江,于是被迫代替她去往敵國和親锦茁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 在C語言中,五種基本數(shù)據(jù)類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,342評論 0 2
  • 關(guān)于使用python實現(xiàn)RSA加密解密 一绣硝、非對稱加密算法 1蜻势、乙方生成兩把密鑰(公鑰和私鑰)撑刺。公鑰是公開的鹉胖,任何...
    ttaymm閱讀 938評論 0 0
  • 專業(yè)考題類型管理運行工作負責人一般作業(yè)考題內(nèi)容選項A選項B選項C選項D選項E選項F正確答案 變電單選GYSZ本規(guī)程...
    小白兔去釣魚閱讀 8,988評論 0 13
  • 課堂應(yīng)急處理技術(shù)(原創(chuàng)) —教學反思 本周學校安排學生進行中招實驗操作練習,我的課不打招呼情況下,無償支援了學校的...
    你健康我快樂_61fc閱讀 286評論 1 3
  • 我想在我有生之年甫菠,好好愛挠铲,好好愛自己,好好愛爸媽寂诱,好好愛世界拂苹。好好愛,忽然眼角灑滿了淚水痰洒,
    左爾于Janean閱讀 176評論 0 0