Inspirated

 
 

June 30, 2008

Programming Challenges: Minesweeper

Filed under: Blog — krkhan @ 8:12 am

PC/UVa IDs: 110102/10189, Popularity: A, Success rate: high, Level: 1

Hello two-dimensional vectors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>
#include <iterator>
#include <sstream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
	int rows, cols, counter;
 
	counter = 1;
	cin>>rows>>cols;
	while(rows != 0 && cols != 0) {
		vector< vector< char > > vc;
 
		for(int i = 0; i < rows; i++) {
			vector< char > row;
			char c;
			for(int j = 0; j < cols; j++) {
				cin>>c;
				row.push_back(c);
			}
			vc.push_back(row);
		}
 
		vector< vector< int > > vi;
 
		for(int i = 0; i < rows; i++) {
			vector< int > row;
			for(int j = 0; j < cols; j++) {
				row.push_back(0);
			}
			vi.push_back(row);
		}
 
		for(int i = 0; i < rows; i++) {
			for(int j = 0; j < cols; j++) {
				if(vc.at(i).at(j) == '*') {
					if(i > 0) {
						vi.at(i - 1).at(j) += 1;
					}
					if(i < rows - 1) {
						vi.at(i + 1).at(j) += 1;
					}
					if(j > 0) {
						vi.at(i).at(j - 1) += 1;
					}
					if(j < cols - 1) {
						vi.at(i).at(j + 1) += 1;
					}
					if(i > 0 && j > 0) {
						vi.at(i - 1).at(j - 1) += 1;
					}
					if(i < rows - 1 && j > 0) {
						vi.at(i + 1).at(j - 1) += 1;
					}
					if(i > 0 && j < cols - 1) {
						vi.at(i - 1).at(j + 1) += 1;
					}
					if(i < rows - 1 && j < cols - 1) {
						vi.at(i + 1).at(j + 1) += 1;
					}
				}
			}
		}
 
		if(counter > 1) {
			cout<<endl;
		}
		cout<<"Field #"<<counter<<":"<<endl;
		for(int i = 0; i < rows; i++) {
			for(int j = 0; j < cols; j++) {
				if(vc.at(i).at(j) == '*') {
					cout<<"*";
				} else {
					cout<<vi.at(i).at(j)<<"";
				}
			}
			cout<<endl;
		}
 
		counter++;
		cin>>rows>>cols;
	}
 
	return 0;
}
Tags: , , , , ,

2 Comments

  1. Damn mass features. I accidentally deleted the most recent comments of my blog after selecting all comments by mistake. Comments were lost on this post too :-( .

    Comment by krkhan — July 18, 2008 @ 8:20 am

  2. great..thanks.. its help me to learn algorithm.. ^_^

    Comment by Maulana Wahid — April 23, 2010 @ 10:14 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.