Tuesday, April 21, 2009

Mine Sweeper...........

//////////////////////////////////////////////////////////////////////////////////////////
The goal of the game is to find where all the mines are located within a M × N field.
The game shows a number in a square which tells you how many mines there are
adjacent to that square. Each square has at most eight adjacent squares. The 4 × 4 field
on the left contains two mines, each represented by a “*” character. If we represent the
same field by the hint numbers described above, we end up with the field on the right:
*... ===== *100
.... ===== 2210
.*.. ===== 1*10
.... ===== 1110
Input
The input will consist of an arbitrary number of fields. The first line of each field
contains two integers n and m (0 < n, m ≤ 100) which stand for the number of lines
and columns of the field, respectively. Each of the next n lines contains exactly m
characters, representing the field.
Safe squares are denoted by “.” and mine squares by “*,” both without the quotes.
The first field line where n = m = 0 represents the end of input and should not be
processed.
Output
For each field, print the message Field #x: on a line alone, where x stands for the
number of the field starting from 1. The next n lines should contain the field with the
“.” characters replaced by the number of mines adjacent to that square. There must
be an empty line between field outputs.
Sample Input ===== Sample Output
4 4 ===== Field #1:
*... ===== *100
.... ===== 2210
.*.. ===== 1*10
.... ===== 1110
3 5=====Field #2:
**... ===== **100
..... ======33200
.*...===== 1*100
0 0
////////////////////////////////////////////////////////////////////////////////////////////

//===============================================================================//
#include
#include

using namespace std;

int matrix[20][20];

void locate(int i,int j,int row,int column)
{
if(i-1 >=0 && i-1 < row && j-1 >=0 && j-1 < column && matrix[i-1][j-1] ==-1)
matrix[i][j] ++;
if(i-1 >=0 && i-1 < row && j >=0 && j < column && matrix[i-1][j] ==-1)
matrix[i][j]++;
if(i-1 >=0 && i-1 < row && j+1 >=0 && j+1 < column && matrix[i-1][j+1] ==-1)
matrix[i][j]++;
if(i >=0 && i < row && j-1 >=0 && j-1 < column && matrix[i][j-1] ==-1)
matrix[i][j]++;
if(i >=0 && i < row && j+1 >=0 && j+1 < column && matrix[i][j+1] ==-1)
matrix[i][j]++;
if(i+1 >=0 && i+1 < row && j-1 >=0 && j-1 < column && matrix[i+1][j-1] ==-1)
matrix[i][j]++;
if(i+1 >=0 && i+1 < row && j >=0 && j < column && matrix[i+1][j] ==-1)
matrix[i][j]++;
if(i+1 >=0 && i+1 < row && j+1 >=0 && j+1 < column && matrix[i+1][j+1] ==-1)
matrix[i][j]++;
}

void locatebombs(int row,int column)
{
for(int i=0;i < row;i++)
{
for(int j=0;j {
if(matrix[i][j] !=-1)
locate(i,j,row,column);
}
}
}

int main()
{
int row,column;
FILE *fp;
freopen("input.txt","r",stdin);
cin>>row>>column;
scanf("\n");
char m[20][20];
for(int i=0;i<=row;i++)
gets(m[i]);
cout< for(int i=0;i {
for(int j=0;j {
if(m[i][j] == '.')
matrix[i][j] = 0;
else
matrix[i][j] = -1;
}
cout< }
for(int i=0;i {
for(int j=0;j cout< cout< }

cout<<"\n"< locatebombs(row,column);
for(int i=0;i {
for(int j=0;j if(matrix[i][j] !=-1)
cout< else
cout<<"*\t";
cout< }
return 0;
}
//====================================================================================//

Thursday, April 16, 2009

Recursive Method to Find Total no of ways in which a positive number K can be written as sum of integers smaller than K

consider the a number 5. It can be represented as following sets {1+1+1+1+1},{1+1+1+2},{1+2+2},{1+1+3},{1+4},{2+3},{5+0} hence there are total seven.

recursive solution for finding total no of ways...

T(n) = Sigma (-1)^(m+1)*[ T(n-m*(3*m-1)/2) + T (n-m*(3*m+1)/2)]

where m = 1,2,3.........until (n-m*(3*m+1)/2) > 0;
Example:
T(5) = T(4) +T(3) - T(0)

//------------------------------------------------------------------------------------------------------------//
          int T(int n)
{
if(n < 0)
return 0;
else
{
if(n == 0)
return 1;
else
{
int temp = 1;
int tempr = 0;
int m = 0;
while(temp > 0)
{
m++;
temp = n-m*(3*m+1)/2;
tempr += pow(-1,m+1)*T(n-m*(3*m-1)/2) +
pow(-1,m+1)*T(n-m*(3*m+1)/2);
}
return tempr;
}
}
}

int main()
{
int number;
scanf("%d",number;);
printf("\nTotal ways = %d",T(number));
}

//------------------------------------------------------------------------------------------------------------//

Wednesday, April 15, 2009

Network Packet sniffing........

A "Packet Sniffer" is a utility that sniffs without modifying or redirecting the data packets. Packet sniffers merely watch, display, and log the traffic of Network.

Network Adapter Card:
Network adapter works in two modes......
Non-promiscuous: Network adapters running in this mode receive only those data packets which is coming for the host computer.
Promiscuous mode: Network adapters running in promiscuous mode receive not only the data directed to the machine hosting, but also all of the traffic on the physically connected local network.

Network Type:
Non-Switched: Sniffing is easy if the network is non-switched. In Non-switched network hub is used. hence each host recieves all the packets coming in the network but discards those which is not coming for it(In non-promiscuous).
Switched: In switched network sniffer is little bit tricky. It can be done by flooding ARP requests which will cause the switch to start behaving like a hub, or other trick that causes switch to redirect traffic to the sniffer system.

ARP Protocol
Address Resolution Protocol (ARP) is a stateless protocol, was designed to map Internet Protocol addresses (IP) to their associated Media Access Control (MAC) addresses. It does not required authentication.

ARP Cache Poisoning:
Broadcasting forged ARP replies on a local network. In a sense, "fooling" nodes on the network. This can be done because ARP lacks authentication features, thus blindly accepting any request and reply that is received or sent.

MAC Address Flooding:
An ARP cache poisoning attack that is mainly used in switched environments. By flooding a switch with fake MAC addresses, a switch is overloaded. Because of
this, it broadcasts all network traffic to every connected node. This outcome is referred to as "broadcast mode" because, all traffic passing through the switch is broadcasted out like a Hub would do. This then can result in sniffing all network traffic.

Packet Filter:
The Linux kernel implements a generic-purpose protocol, called PF_PACKET, which allows to create a socket that receives packets directly from the network card driver. Hence, any other protocols' handling is skipped, and any packets can be received.

Search Ranjeet's Blog