UVa OJ 1347 - Tour
Problem
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xi, yi > . John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is ...
std::ios_base::sync_with_stdio()
std::ios_base::sync_with_stdio()
今天在做OJ的时候意外地发现printf和cout的输出顺序和代码顺序不一样,如下
123printf("Case Number %d: ", ++cas);if(takeTime[0][1] >= 0x3f3f3f3f) cout << "impossible\n";else cout << takeTime[0][1] << '\n';
得到的输出却是
1234impossibleimpossibleimpossibleCase Number 1: Case Number 2: Case Number 3:
后来仔细地看了看,才发现问题是出在ios_base::sync_with_stdio()这个函数上。
这个函数当我们填入false的值时,相当于把C和C++的输入输出流解绑了,那么cout就能够拥有自己的缓冲区。如果我们去掉解绑的话,那么我们就会看到结果变得和我们预想的是一样了。
123Case Number 1 ...
UVa OJ 714 - Copying Books
Problem
Here is the Problem Link
Solution
在车站有三种选择:1.等;2.向右走 3.向左走
我们从约定的地点和约定的时间倒回来考虑,如果能够在0(初始时刻)回到车站1,就表示能够完成。
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162#include <iostream>#include <cstring>#include <cstdio>using namespace std;int n, T, m1, m2, cas;;int t[55], d[255], e[255];int fromRight[205][55], fromLeft[205][55], takeTime[205][55];void init() { memset(fromLeft, 0, sizeof(fromLeft )); mem ...
UVa OJ 437 - The Tower of Babylon
Problem
The babylonians had n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.
They wanted to construct the tallest tower possible by stacking blocks. The problem was that, in building a tower, one block could only be placed on top of another block as long as ...
UVa OJ 12265 - Selling Land
Problem
输入一个n*m(1≤n,m≤1000)矩阵,每个格子可能是空地,也可能是沼泽。对于每个空地格子,求出以它为右下角的空矩形的最大周长,然后统计每个周长出现了多少次。
Input
On the first line a positive integer: the number of test cases, at most 100. After that per test case:
One line with two integers n and m (1 ≤ n, m ≤ 1000): the dimensions of Per’s parcel.
n lines, each with m characters. Each character is either ‘#’ or ‘.’. The j-th character on the i-th line is a ‘#’ if position (i, j) is a swamp, and ‘.’ if it is grass. The north-west corner of Per’s parcel has co ...
UVa OJ 1442 - Cav
Problem
Link: 1442 - Cav
Solution
Use greedy algorithm to deal with this problem.
Adjust the height of ceiling to fit the requirements.
Here is the code:
123456789101112131415161718192021222324252627282930313233343536#include <iostream>#include <cstdio>using namespace std;const int maxn = 1e6+5;int cas, n, cnt;int ceiling[maxn], floor[maxn];int main() { ios::sync_with_stdio(false); cin.tie(0); //freopen("input.txt" , "r", stdin ); //freopen(" ...
The Python Challenge Level-11 Solution
先附上我在Github上存放的代码仓库: The Python Challenge
这个题目感觉就很玄乎了,就只给我们一张图片,网页源码里空空如也
这里实际上是把一张图片拆分成两张来看。
我尝试了不同的奇偶拆分方式,最终还是选定了X轴和Y轴相加的方式来判断奇偶
1234567891011121314151617from io import BytesIOfrom PIL import Image__author__ = 'Yuuki_Dach'img = Image.open('cave.jpg')width, height = img.sizeeven = Image.new('RGB', (width >> 1, height >> 1))odd = Image.new('RGB', (width >> 1, height >> 1))for i in range(width): for j in range(height): imgP ...
The Python Challenge Level 9-10 Solutions
先附上我在Github上存放的代码仓库: The Python Challenge
Level 9
第九关树的图片其实是一个提示,就是要我把点一个个的连起来,而这个点是由网页源代码里注释的first和second部分给出的,我直接把两个部分的坐标提取到一起,能够得到一个牛的图片
1234567891011121314151617import requestsimport refrom PIL import Image, ImageDrawwebUrl = 'http://www.pythonchallenge.com/pc/return/good.html'webContent = requests.get(webUrl, auth=('huge','file')).textprint(webContent)pattern = re.compile(r"(\d{2,3})")nums = re.findall(pattern, webContent)nums = list(map(int ...
The Python Challenge Level-8 Solution
先附上我在Github上存放的代码仓库: The Python Challenge
这道题目消耗了我相当长的一段时间,让我对str和byte也有了一个新的认识。
首先还是老样子,进入网页源代码,看看里面有什么
首先引入眼帘的是一大串坐标代码,加上第七题的做法,很容易让人产生误会。我也试着去把他门转换了一下,发现并没有什么用处,然后就把目光集中在了里面的链接上,点了一下,需要用户名和密码,随便输入了123,并没有什么效果
再看看源代码底部有’un’和’pw’,刚好两个,而且和’user name’ 'password’相对应。
但是这两串字符又有什么意义呢?
我查了一下,发现这实际上是经bz2压缩后的信息,于是开始编写程序解码
12345678910#! /usr/bin/env python3# -*- coding: utf-8 -*-__author__ = 'Yuuki_Dach'import bz2un = b"BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x ...
The Python Challenge Level-7 Solution
先附上我在Github上存放的代码仓库: The Python Challenge
这道题目网页源代码里没有什么别的提示,而图片中有个条形码类似物,那么就需要我们对图片进行处理了。先想办法把条形码读出来,并且转换成可读的文字
123456789101112#! /usr/bin/env python3# -*- coding: utf-8 -*-from PIL import Imagefrom io import BytesIOimport requestsimgUrl = 'http://www.pythonchallenge.com/pc/def/oxygen.png'img = Image.open(BytesIO(requests.get(imgUrl).content))for i in range(img.width): midPixel = img.getpixel((i,img.height>>1)) print(midPixel)
得到结果以后我们可以发现,条形码中没一条的宽度是7个像素,所以我们可以再处理一下,把相同的条 ...